Skip to content

Commit

Permalink
fix: Tag component primitive + documentation; + fixes usage bugs (#818
Browse files Browse the repository at this point in the history
)

Co-authored-by: Andrew Jiang <[email protected]>
  • Loading branch information
KenzoBenzo and abvthecity authored May 7, 2024
1 parent de7192b commit 37455f8
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 120 deletions.
6 changes: 3 additions & 3 deletions packages/ui/app/src/api-page/endpoints/EndpointContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dynamic from "next/dynamic";
import { useRouter } from "next/router";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useInView } from "react-intersection-observer";
import { withStream } from "../../commons/withStream";
import { withStream } from "../../commons/HttpMethodTag";
import { useDocsContext } from "../../contexts/docs-context/useDocsContext";
import { useLayoutBreakpoint } from "../../contexts/layout-breakpoint/useLayoutBreakpoint";
import { useViewportSize } from "../../hooks/useViewportSize";
Expand Down Expand Up @@ -225,9 +225,9 @@ export const EndpointContent: React.FC<EndpointContent.Props> = ({
<Breadcrumbs breadcrumbs={breadcrumbs} />
<div>
{endpoint.responseBody?.shape.type === "stream" ? (
withStream(<h1 className="my-0 inline leading-tight">{endpoint.title}</h1>)
withStream(<h1 className="my-0 inline leading-none">{endpoint.title}</h1>, "lg")
) : (
<h1 className="my-0 inline leading-tight">{endpoint.title}</h1>
<h1 className="my-0 inline leading-none">{endpoint.title}</h1>
)}
{endpoint.availability != null && (
<span className="relative">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function ErrorCodeSnippetExample({
<CodeSnippetExample
title={
<span className="inline-flex items-center">
<FernTag intent="danger">{resolvedError.statusCode}</FernTag>
<FernTag colorScheme="red">{resolvedError.statusCode}</FernTag>
{options.length === 0 ? (
<span className="ml-2 text-sm text-intent-danger">{resolvedError.name}</span>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChevronDownIcon, SlashIcon } from "@radix-ui/react-icons";
import { TooltipProvider } from "@radix-ui/react-tooltip";
import cn from "clsx";
import { FC, Fragment, useCallback, useRef } from "react";
import { withStream } from "../commons/withStream";
import { withStream } from "../commons/HttpMethodTag";
import { FernButton } from "../components/FernButton";
import { usePlaygroundContext } from "./PlaygroundContext";
import { ApiGroup, PlaygroundEndpointSelectorContent } from "./PlaygroundEndpointSelectorContent";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import cn from "clsx";
import { noop } from "lodash-es";
import dynamic from "next/dynamic";
import { Fragment, ReactElement, forwardRef, useImperativeHandle, useRef, useState } from "react";
import { HttpMethodTag } from "../commons/HttpMethodTag";
import { withStream } from "../commons/withStream";
import { Chip } from "../components/Chip";
import { HttpMethodTag, withStream } from "../commons/HttpMethodTag";
import { FernButton } from "../components/FernButton";
import { FernInput } from "../components/FernInput";
import { FernScrollArea } from "../components/FernScrollArea";
Expand Down Expand Up @@ -154,7 +152,7 @@ export const PlaygroundEndpointSelectorContent = forwardRef<HTMLDivElement, Play
intent={active ? "primary" : "none"}
active={active}
onClick={createSelectEndpoint(apiGroup, endpointItem)}
rightIcon={<HttpMethodTag method={endpointItem.method} small={true} />}
rightIcon={<HttpMethodTag method={endpointItem.method} size="sm" />}
/>
</FernTooltip>
</li>
Expand All @@ -177,7 +175,7 @@ export const PlaygroundEndpointSelectorContent = forwardRef<HTMLDivElement, Play
intent={active ? "primary" : "none"}
active={active}
onClick={createSelectWebSocket(apiGroup, endpointItem)}
rightIcon={<Chip name="WSS" small />}
rightIcon={<HttpMethodTag method="WSS" />}
/>
</FernTooltip>
</li>
Expand Down
81 changes: 50 additions & 31 deletions packages/ui/app/src/commons/HttpMethodTag.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
import { FdrAPI } from "@fern-api/fdr-sdk";
import cn from "clsx";
import { memo } from "react";
import clsx from "clsx";
import { ReactNode, memo } from "react";
import { FernTag, FernTagColorScheme, FernTagProps } from "../components/FernTag";
import { FernTooltip } from "../components/FernTooltip";

export declare namespace HttpMethodTag {
export interface Props {
method: FdrAPI.api.v1.read.HttpMethod;
small?: boolean;
className?: string;
export interface Props extends FernTagProps {
method: FdrAPI.api.v1.read.HttpMethod | "STREAM" | "WSS";
active?: boolean;
}
}

const UnmemoizedHttpMethodTag: React.FC<HttpMethodTag.Props> = ({ method, small = false, className, active }) => {
const METHOD_COLOR_SCHEMES: Record<HttpMethodTag.Props["method"], FernTagColorScheme> = {
GET: "green",
DELETE: "red",
POST: "blue",
STREAM: "accent",
WSS: "accent",
PUT: "amber",
PATCH: "amber",
};

const UnmemoizedHttpMethodTag: React.FC<HttpMethodTag.Props> = ({
method,
active,
size = "sm",
className,
...rest
}) => {
return (
<span
className={cn(className, "uppercase font-mono inline-flex justify-center items-center leading-none", {
["bg-tag-method-get text-text-method-get dark:text-method-get-dark"]:
method === FdrAPI.api.v1.read.HttpMethod.Get && !active,
["bg-method-get text-text-default-inverted"]: method === FdrAPI.api.v1.read.HttpMethod.Get && active,
["bg-tag-method-post text-text-method-post"]: method === FdrAPI.api.v1.read.HttpMethod.Post && !active,
["bg-method-post text-text-default-inverted"]: method === FdrAPI.api.v1.read.HttpMethod.Post && active,
["bg-tag-method-delete text-text-method-delete"]:
method === FdrAPI.api.v1.read.HttpMethod.Delete && !active,
["bg-method-delete text-text-default-inverted"]:
method === FdrAPI.api.v1.read.HttpMethod.Delete && active,
["bg-tag-method-put text-text-method-put"]: method === FdrAPI.api.v1.read.HttpMethod.Put && !active,
["bg-method-put text-text-default-inverted"]: method === FdrAPI.api.v1.read.HttpMethod.Put && active,
["bg-tag-method-patch text-text-method-patch"]:
method === FdrAPI.api.v1.read.HttpMethod.Patch && !active,
["bg-method-patch text-text-default-inverted"]:
method === FdrAPI.api.v1.read.HttpMethod.Patch && active,
["rounded-md h-[18px] text-[10px] w-9"]: small,
["py-1 px-2 rounded-lg h-6 text-xs"]: !small,
})}
style={{
lineHeight: 1,
}}
<FernTag
colorScheme={METHOD_COLOR_SCHEMES[method]}
variant={active ? "solid" : "subtle"}
className={clsx("uppercase", { "w-11": size === "sm" }, className)}
size={size}
{...rest}
>
{method === FdrAPI.api.v1.read.HttpMethod.Delete ? "DEL" : method}
</span>
</FernTag>
);
};

export function withStream(text: ReactNode, size: "sm" | "lg" = "sm"): ReactNode {
return (
<span className="inline-flex items-center gap-2">
<span>{text}</span>
<UnmemoizedHttpMethodTag size={size} method="STREAM" />
</span>
);
}
export function withWss(text: ReactNode, size: "sm" | "lg" = "sm"): ReactNode {
return (
<span className="inline-flex items-center gap-2">
<span>{text}</span>
<FernTooltip content="WebSocket Channel">
<UnmemoizedHttpMethodTag size={size} method="WSS" />
</FernTooltip>
</span>
);
}

export const HttpMethodTag = memo(UnmemoizedHttpMethodTag);
26 changes: 0 additions & 26 deletions packages/ui/app/src/commons/withStream.tsx

This file was deleted.

29 changes: 0 additions & 29 deletions packages/ui/app/src/commons/withWss.tsx

This file was deleted.

108 changes: 108 additions & 0 deletions packages/ui/app/src/components/FernTag.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import type { Meta, StoryObj } from "@storybook/react";
import { FernTag, FernTagColorSchemes, FernTagSizes } from "./FernTag";

const methods = ["test", "get", "post", "put", "patch", "delete", "stream", "wss"];

const meta: Meta<typeof FernTag> = {
title: "General/FernTag",
component: FernTag,
parameters: {
layout: "centered",
},
tags: ["autodocs"],
args: {
children: "Test",
size: "lg",
variant: "subtle",
colorScheme: "gray",
},
argTypes: {
size: {
options: ["sm", "lg"],
control: { type: "select" },
},
},
};

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {},
};

export const ColorSchemes: Story = {
argTypes: {
colorScheme: {
control: false,
},
},
render: (args) => {
const colorSchemes = Object.values(FernTagColorSchemes);
return (
<div className="flex flex-col gap-2">
{colorSchemes.map((colorScheme, i) => (
<FernTag key={i} {...args} colorScheme={colorScheme} />
))}
</div>
);
},
};

export const Sizes: Story = {
argTypes: {
size: {
control: false,
},
},
render: (args) => {
const sizes = Object.values(FernTagSizes);
return (
<div className="flex gap-2">
{sizes.map((size, i) => (
<FernTag key={i} {...args} size={size} />
))}
</div>
);
},
};

export const Solid: Story = {
args: {
variant: "solid",
},
};

export const ClassNameOverrides: Story = {
args: {
size: "sm",
className: "w-11 uppercase",
},
render: (args) => {
return (
<div className="flex flex-col gap-2">
{methods.map((method) => (
<FernTag
key={method}
{...args}
colorScheme={
method === "GET"
? "green"
: method === "DELETE"
? "red"
: method === "POST"
? "blue"
: method === "STREAM" || method === "WSS"
? "accent"
: method === "PUT" || method === "PATCH"
? "amber"
: "gray"
}
>
{method === "DELETE" ? "DEL" : method}
</FernTag>
))}
</div>
);
},
};
Loading

0 comments on commit 37455f8

Please sign in to comment.