diff --git a/r/tile-picker.json b/r/tile-picker.json index 26457844..be4f1739 100644 --- a/r/tile-picker.json +++ b/r/tile-picker.json @@ -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, \"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 }) {\n const [search, setSearch] = React.useState(\"\");\n const inputRef = React.useRef(null);\n const query = search.trim().toLowerCase();\n const popular = query ? [] : items.filter((item) => item.popular);\n const grid = (list: readonly TilePickerItem[]) => (\n
\n {list.map((item) => (\n {item.media}}\n onClick={() => onSelect(item.value)}\n />\n ))}\n
\n );\n const section = (label: string, list: readonly TilePickerItem[]) => (\n
\n

{label}

\n {grid(list)}\n
\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 = {empty};\n else if (query && found.length === 0)\n body = (\n {\n setSearch(\"\");\n inputRef.current?.focus();\n }}\n >\n Clear search\n \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
\n 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
\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, \"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 }) {\n const [search, setSearch] = React.useState(\"\");\n const inputRef = React.useRef(null);\n const query = search.trim().toLowerCase();\n const popular = query ? [] : items.filter((item) => item.popular);\n const grid = (list: readonly TilePickerItem[]) => (\n
\n {list.map((item) => (\n {item.media}}\n onClick={() => onSelect(item.value)}\n />\n ))}\n
\n );\n const section = (label: string, list: readonly TilePickerItem[]) => (\n
\n

{label}

\n {grid(list)}\n
\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 = {empty};\n else if (query && found.length === 0)\n body = (\n <>\n {\n setSearch(\"\");\n inputRef.current?.focus();\n }}\n >\n Clear search\n \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
\n 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
\n );\n}\nTilePicker.displayName = \"TilePicker\";\n" }, { "path": "src/components/cipher/patterns/tile-picker.module.css", diff --git a/site/src/catalog.json b/site/src/catalog.json index bb03721e..b5e5dfdc 100644 --- a/site/src/catalog.json +++ b/site/src/catalog.json @@ -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" diff --git a/site/src/examples/tile-picker/pinned.tsx b/site/src/examples/tile-picker/pinned.tsx new file mode 100644 index 00000000..d8ad2eac --- /dev/null +++ b/site/src/examples/tile-picker/pinned.tsx @@ -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 ( + {}} + searchLabel="Search services" + allLabel="All services" + noun="services" + /> + ); +} diff --git a/src/patterns/tile-picker.tsx b/src/patterns/tile-picker.tsx index 69d1d7b0..e60b65b9 100644 --- a/src/patterns/tile-picker.tsx +++ b/src/patterns/tile-picker.tsx @@ -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, "onSelect"> & { @@ -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, @@ -93,29 +95,33 @@ export function TilePicker({ ); 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 = {empty}; else if (query && found.length === 0) body = ( - { - setSearch(""); - inputRef.current?.focus(); - }} - > - Clear search - - } - /> + <> + { + setSearch(""); + inputRef.current?.focus(); + }} + > + Clear search + + } + /> + {pinned.length > 0 && grid(pinned)} + ); else if (popular.length > 0) body = ( @@ -124,7 +130,7 @@ export function TilePicker({ {section(allLabel, items)} ); - else body = grid(found); + else body = grid([...found, ...pinned]); return (