Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion r/tile-picker.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"path": "src/components/cipher/patterns/tile-picker.tsx",
"type": "registry:file",
"target": "src/components/cipher/patterns/tile-picker.tsx",
"content": "import * as React from \"react\";\n\nimport { cn } from \"../lib/utils\";\nimport { Button } from \"../ui/button\";\nimport { type EmptyGhost, EmptyGhostScope, EmptyState } from \"../ui/empty\";\nimport type { IconSource } from \"../ui/icon\";\nimport { Input } from \"../ui/input\";\nimport { Media, Tile } from \"../ui/item\";\n\nimport styles from \"./tile-picker.module.css\";\n\nexport type TilePickerItem = {\n value: string;\n title: string;\n description?: string;\n /** A short group name, such as AWS or CI/CD, shown across from the media. */\n eyebrow?: string;\n icon?: IconSource;\n /** A logo or image, framed like an icon. Ignored when icon is set. */\n media?: React.ReactNode;\n /** Also listed under the popular section while the search is empty. */\n popular?: boolean;\n disabledReason?: string;\n /** Extra words the search matches, such as a vendor's other product names. */\n keywords?: readonly string[];\n};\n\nexport type TilePickerProps = Omit<React.ComponentPropsWithoutRef<\"div\">, \"onSelect\"> & {\n items: readonly TilePickerItem[];\n onSelect: (value: string) => void;\n searchLabel?: string;\n searchPlaceholder?: string;\n popularLabel?: string;\n /** Heads the full list when popular items are shown above it. */\n allLabel?: string;\n /** Names what the search found nothing of, as in No items match. */\n noun?: string;\n /** Shown in place of the grid when there are no items at all, such as an EmptyState. */\n empty?: React.ReactNode;\n /** The ghost behind an Empty in the empty slot. Defaults to cards; a search that finds nothing never draws one. */\n ghost?: EmptyGhost;\n};\n\nconst matches = (item: TilePickerItem, query: string) =>\n [item.title, item.eyebrow, item.description, ...(item.keywords ?? [])].some((text) =>\n text?.toLowerCase().includes(query)\n );\n\n/**\n * Search plus a responsive grid of stacked Tiles. With an empty search, popular items come first\n * under their own label and every item follows; a search lists its matches flat, and finding\n * nothing offers to clear it. The grid adds columns as the container widens, so it fits a sheet\n * or a page without a frame of its own.\n */\nexport function TilePicker({\n items,\n onSelect,\n searchLabel = \"Search\",\n searchPlaceholder,\n popularLabel = \"Popular\",\n allLabel = \"All\",\n noun = \"items\",\n empty,\n ghost,\n className,\n ref,\n ...props\n}: TilePickerProps & { ref?: React.Ref<HTMLDivElement> }) {\n const [search, setSearch] = React.useState(\"\");\n const inputRef = React.useRef<HTMLInputElement>(null);\n const query = search.trim().toLowerCase();\n const popular = query ? [] : items.filter((item) => item.popular);\n const grid = (list: readonly TilePickerItem[]) => (\n <div className={styles.grid}>\n {list.map((item) => (\n <Tile\n key={item.value}\n layout=\"stacked\"\n title={item.title}\n description={item.description}\n eyebrow={item.eyebrow}\n disabledReason={item.disabledReason}\n media={(item.icon || item.media) && <Media icon={item.icon}>{item.media}</Media>}\n onClick={() => onSelect(item.value)}\n />\n ))}\n </div>\n );\n const section = (label: string, list: readonly TilePickerItem[]) => (\n <section className={styles.section} aria-label={label}>\n <h3 className={styles.label}>{label}</h3>\n {grid(list)}\n </section>\n );\n const found = query ? items.filter((item) => matches(item, query)) : items;\n let body: React.ReactNode;\n if (items.length === 0 && empty !== undefined)\n body = <EmptyGhostScope ghost={ghost ?? \"cards\"}>{empty}</EmptyGhostScope>;\n else if (query && found.length === 0)\n body = (\n <EmptyState\n ghost={false}\n size=\"sm\"\n media=\"bare\"\n icon=\"search\"\n title={`No ${noun} match \"${search.trim()}\"`}\n actions={\n <Button\n variant=\"outline\"\n onClick={() => {\n setSearch(\"\");\n inputRef.current?.focus();\n }}\n >\n Clear search\n </Button>\n }\n />\n );\n else if (popular.length > 0)\n body = (\n <>\n {section(popularLabel, popular)}\n {section(allLabel, items)}\n </>\n );\n else body = grid(found);\n return (\n <div ref={ref} data-slot=\"tile-picker\" className={cn(styles.picker, className)} {...props}>\n <Input\n ref={inputRef}\n icon=\"search\"\n aria-label={searchLabel}\n placeholder={searchPlaceholder ?? searchLabel}\n value={search}\n data-cipher-escape-owner={search ? \"\" : undefined}\n onChange={(event) => setSearch(event.target.value)}\n onKeyDown={(event) => {\n if (event.key === \"Escape\" && search) {\n event.preventDefault();\n event.stopPropagation();\n setSearch(\"\");\n }\n }}\n />\n {body}\n </div>\n );\n}\nTilePicker.displayName = \"TilePicker\";\n"
"content": "import * as React from \"react\";\n\nimport { cn } from \"../lib/utils\";\nimport { Button } from \"../ui/button\";\nimport { type EmptyGhost, EmptyGhostScope, EmptyState } from \"../ui/empty\";\nimport type { IconSource } from \"../ui/icon\";\nimport { Input } from \"../ui/input\";\nimport { Media, Tile } from \"../ui/item\";\n\nimport styles from \"./tile-picker.module.css\";\n\nexport type TilePickerItem = {\n value: string;\n title: string;\n description?: string;\n /** A short group name, such as AWS or CI/CD, shown across from the media. */\n eyebrow?: string;\n icon?: IconSource;\n /** A logo or image, framed like an icon. Ignored when icon is set. */\n media?: React.ReactNode;\n /** Also listed under the popular section while the search is empty. */\n popular?: boolean;\n disabledReason?: string;\n /** Extra words the search matches, such as a vendor's other product names. */\n keywords?: readonly string[];\n /** Always listed, after any matches, even when a search doesn't match it. For catch-alls such as a custom integration. */\n pinned?: boolean;\n};\n\nexport type TilePickerProps = Omit<React.ComponentPropsWithoutRef<\"div\">, \"onSelect\"> & {\n items: readonly TilePickerItem[];\n onSelect: (value: string) => void;\n searchLabel?: string;\n searchPlaceholder?: string;\n popularLabel?: string;\n /** Heads the full list when popular items are shown above it. */\n allLabel?: string;\n /** Names what the search found nothing of, as in No items match. */\n noun?: string;\n /** Shown in place of the grid when there are no items at all, such as an EmptyState. */\n empty?: React.ReactNode;\n /** The ghost behind an Empty in the empty slot. Defaults to cards; a search that finds nothing never draws one. */\n ghost?: EmptyGhost;\n};\n\nconst matches = (item: TilePickerItem, query: string) =>\n [item.title, item.eyebrow, item.description, ...(item.keywords ?? [])].some((text) =>\n text?.toLowerCase().includes(query)\n );\n\n/**\n * Search plus a responsive grid of stacked Tiles. With an empty search, popular items come first\n * under their own label and every item follows; a search lists its matches flat, then any\n * pinned items, and finding nothing offers to clear it above the pinned items. The grid adds\n * columns as the container widens, so it fits a sheet or a page without a frame of its own.\n */\nexport function TilePicker({\n items,\n onSelect,\n searchLabel = \"Search\",\n searchPlaceholder,\n popularLabel = \"Popular\",\n allLabel = \"All\",\n noun = \"items\",\n empty,\n ghost,\n className,\n ref,\n ...props\n}: TilePickerProps & { ref?: React.Ref<HTMLDivElement> }) {\n const [search, setSearch] = React.useState(\"\");\n const inputRef = React.useRef<HTMLInputElement>(null);\n const query = search.trim().toLowerCase();\n const popular = query ? [] : items.filter((item) => item.popular);\n const grid = (list: readonly TilePickerItem[]) => (\n <div className={styles.grid}>\n {list.map((item) => (\n <Tile\n key={item.value}\n layout=\"stacked\"\n title={item.title}\n description={item.description}\n eyebrow={item.eyebrow}\n disabledReason={item.disabledReason}\n media={(item.icon || item.media) && <Media icon={item.icon}>{item.media}</Media>}\n onClick={() => onSelect(item.value)}\n />\n ))}\n </div>\n );\n const section = (label: string, list: readonly TilePickerItem[]) => (\n <section className={styles.section} aria-label={label}>\n <h3 className={styles.label}>{label}</h3>\n {grid(list)}\n </section>\n );\n const found = query ? items.filter((item) => matches(item, query)) : items;\n const pinned = query ? items.filter((item) => item.pinned && !matches(item, query)) : [];\n let body: React.ReactNode;\n if (items.length === 0 && empty !== undefined)\n body = <EmptyGhostScope ghost={ghost ?? \"cards\"}>{empty}</EmptyGhostScope>;\n else if (query && found.length === 0)\n body = (\n <>\n <EmptyState\n ghost={false}\n size=\"sm\"\n media=\"bare\"\n icon=\"search\"\n title={`No ${noun} match \"${search.trim()}\"`}\n actions={\n <Button\n variant=\"outline\"\n onClick={() => {\n setSearch(\"\");\n inputRef.current?.focus();\n }}\n >\n Clear search\n </Button>\n }\n />\n {pinned.length > 0 && grid(pinned)}\n </>\n );\n else if (popular.length > 0)\n body = (\n <>\n {section(popularLabel, popular)}\n {section(allLabel, items)}\n </>\n );\n else body = grid([...found, ...pinned]);\n return (\n <div ref={ref} data-slot=\"tile-picker\" className={cn(styles.picker, className)} {...props}>\n <Input\n ref={inputRef}\n icon=\"search\"\n aria-label={searchLabel}\n placeholder={searchPlaceholder ?? searchLabel}\n value={search}\n data-cipher-escape-owner={search ? \"\" : undefined}\n onChange={(event) => setSearch(event.target.value)}\n onKeyDown={(event) => {\n if (event.key === \"Escape\" && search) {\n event.preventDefault();\n event.stopPropagation();\n setSearch(\"\");\n }\n }}\n />\n {body}\n </div>\n );\n}\nTilePicker.displayName = \"TilePicker\";\n"
},
{
"path": "src/components/cipher/patterns/tile-picker.module.css",
Expand Down
4 changes: 4 additions & 0 deletions site/src/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,10 @@
"title": "A few providers",
"module": "tile-picker/few"
},
{
"title": "Pinned items: Custom HTTP and MCP stay listed under any search",
"module": "tile-picker/pinned"
},
{
"title": "Long names and disabled providers",
"module": "tile-picker/stressed"
Expand Down
60 changes: 60 additions & 0 deletions site/src/examples/tile-picker/pinned.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { TilePicker, type TilePickerItem } from "@infisical/cipher/patterns/tile-picker";

const services: TilePickerItem[] = [
{
value: "github",
title: "GitHub",
description: "Repositories, issues and pull requests.",
eyebrow: "Source control",
icon: "git"
},
{
value: "gitlab",
title: "GitLab",
description: "Projects, merge requests and pipelines.",
eyebrow: "Source control",
icon: "git"
},
{
value: "linear",
title: "Linear",
description: "Issues, projects and cycles.",
eyebrow: "Project management",
icon: "folder"
},
{
value: "stripe",
title: "Stripe",
description: "Customers, invoices and subscriptions.",
eyebrow: "Payments",
icon: "key"
},
{
value: "custom-http",
title: "Custom HTTP",
description: "Any HTTP API, with headers and auth you define.",
eyebrow: "Generic",
icon: "cloud",
pinned: true
},
{
value: "mcp",
title: "MCP",
description: "Any server that speaks the Model Context Protocol.",
eyebrow: "Protocol",
icon: "link",
pinned: true
}
];

export default function Example() {
return (
<TilePicker
items={services}
onSelect={() => {}}
searchLabel="Search services"
allLabel="All services"
noun="services"
/>
);
}
50 changes: 28 additions & 22 deletions src/patterns/tile-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type TilePickerItem = {
disabledReason?: string;
/** Extra words the search matches, such as a vendor's other product names. */
keywords?: readonly string[];
/** Always listed, after any matches, even when a search doesn't match it. For catch-alls such as a custom integration. */
pinned?: boolean;
};

export type TilePickerProps = Omit<React.ComponentPropsWithoutRef<"div">, "onSelect"> & {
Expand All @@ -48,9 +50,9 @@ const matches = (item: TilePickerItem, query: string) =>

/**
* Search plus a responsive grid of stacked Tiles. With an empty search, popular items come first
* under their own label and every item follows; a search lists its matches flat, and finding
* nothing offers to clear it. The grid adds columns as the container widens, so it fits a sheet
* or a page without a frame of its own.
* under their own label and every item follows; a search lists its matches flat, then any
* pinned items, and finding nothing offers to clear it above the pinned items. The grid adds
* columns as the container widens, so it fits a sheet or a page without a frame of its own.
*/
export function TilePicker({
items,
Expand Down Expand Up @@ -93,29 +95,33 @@ export function TilePicker({
</section>
);
const found = query ? items.filter((item) => matches(item, query)) : items;
const pinned = query ? items.filter((item) => item.pinned && !matches(item, query)) : [];
let body: React.ReactNode;
if (items.length === 0 && empty !== undefined)
body = <EmptyGhostScope ghost={ghost ?? "cards"}>{empty}</EmptyGhostScope>;
else if (query && found.length === 0)
body = (
<EmptyState
ghost={false}
size="sm"
media="bare"
icon="search"
title={`No ${noun} match "${search.trim()}"`}
actions={
<Button
variant="outline"
onClick={() => {
setSearch("");
inputRef.current?.focus();
}}
>
Clear search
</Button>
}
/>
<>
<EmptyState
ghost={false}
size="sm"
media="bare"
icon="search"
title={`No ${noun} match "${search.trim()}"`}
actions={
<Button
variant="outline"
onClick={() => {
setSearch("");
inputRef.current?.focus();
}}
>
Clear search
</Button>
}
/>
{pinned.length > 0 && grid(pinned)}
</>
);
else if (popular.length > 0)
body = (
Expand All @@ -124,7 +130,7 @@ export function TilePicker({
{section(allLabel, items)}
</>
);
else body = grid(found);
else body = grid([...found, ...pinned]);
return (
<div ref={ref} data-slot="tile-picker" className={cn(styles.picker, className)} {...props}>
<Input
Expand Down
Loading