From b1d7e7b0fc1ed7d263264f8e6dc0ea1aae6db554 Mon Sep 17 00:00:00 2001 From: andrewhuhh Date: Tue, 17 Nov 2026 04:20:51 +0000 Subject: [PATCH] Item: a display: contents button label counts as visible text Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- r/item.json | 2 +- src/ui/item.tsx | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/r/item.json b/r/item.json index de0f809e..c6308150 100644 --- a/r/item.json +++ b/r/item.json @@ -17,7 +17,7 @@ "path": "src/components/cipher/ui/item.tsx", "type": "registry:file", "target": "src/components/cipher/ui/item.tsx", - "content": "import * as React from \"react\";\nimport { cva, type VariantProps } from \"class-variance-authority\";\nimport { Slot } from \"radix-ui\";\n\nimport { Separator } from \"./separator\";\n\nimport { useDescendantFlags } from \"../lib/descendant-flags\";\nimport { useInFrame } from \"../lib/frame-context\";\nimport { cn } from \"../lib/utils\";\nimport { Icon, type IconSource } from \"./icon\";\nimport { Tooltip } from \"./tooltip\";\n\nimport controlStyles from \"./controls.module.css\";\nimport helperStyles from \"./helpers.module.css\";\nimport itemStyles from \"./item.module.css\";\n\nconst ItemGroupContext = React.createContext(false);\n\nconst groupFlags = { \"data-has-sm\": '[data-size=\"sm\"]', \"data-has-xs\": '[data-size=\"xs\"]' };\nconst groupWatch = { attributes: [\"data-size\"] };\nconst itemFlags = { \"data-has-description\": '[data-slot=\"item-description\"]' };\n\nfunction ItemGroup({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n const groupRef = useDescendantFlags(groupFlags, groupWatch, ref);\n return (\n \n \n \n );\n}\nItemGroup.displayName = \"ItemGroup\";\n\nfunction ItemSeparator({ className, ref, ...props }: React.ComponentProps) {\n return (\n \n );\n}\nItemSeparator.displayName = \"ItemSeparator\";\n\nconst itemVariants = cva(\n [\n controlStyles.focusOutline,\n \"group/item flex w-full flex-wrap rounded-lg text-sm transition-colors duration-(--cipher-duration-fast) [a]:transition-colors [a]:hover:bg-[color:var(--cipher-action-ghost-hover)]\"\n ],\n {\n variants: {\n variant: {\n default: \"border border-transparent\",\n outline: \"border border-[color:var(--cipher-line-soft)]\",\n muted: \"border border-transparent bg-muted/50\"\n },\n size: {\n default: \"gap-2.5 px-3 py-2.5\",\n md: \"gap-2.5 px-3 py-2.5\",\n sm: \"gap-2.5 px-3 py-2.5\",\n xs: \"gap-2 px-3 py-2 in-data-[slot=dropdown-menu-content]:p-0\"\n }\n },\n defaultVariants: {\n variant: \"default\",\n size: \"default\"\n }\n }\n);\n\nexport type ItemProps = Omit, \"title\"> &\n VariantProps & {\n asChild?: boolean;\n title?: React.ReactNode;\n description?: React.ReactNode;\n icon?: IconSource;\n media?: React.ReactNode;\n badge?: React.ReactNode;\n meta?: React.ReactNode;\n error?: React.ReactNode;\n /** Buttons at the row end. They sit beside the copy once the row is 36rem wide and stack below it under that. The width is the content box, so an outline Item needs its padding and border on top (about 39rem outside). */\n actions?: React.ReactNode;\n actionsPlacement?: \"end\" | \"below\";\n unread?: boolean;\n resolved?: boolean;\n /**\n * Keeps the title and description on one line with an ellipsis, and puts the full text in a\n * native tooltip. Unset, the title still truncates and the description wraps to two lines\n * before its ellipsis, except on a row with actions at the default or md size, where both\n * wrap. titleLines takes over the title when set, so truncate then governs only the\n * description.\n */\n truncate?: boolean;\n /**\n * Clamps the title to this many lines and shows the full title in a Tooltip while it is cut\n * off. Unset leaves the title to truncate. titleLines={1} reads like truncate but uses a Cipher\n * tooltip instead of a native one.\n */\n titleLines?: number;\n mono?: boolean;\n };\n\nfunction ClampedTitle({\n lines,\n className,\n children\n}: {\n lines: number;\n className?: string;\n children: React.ReactNode;\n}) {\n const [element, setElement] = React.useState(null);\n const [clamped, setClamped] = React.useState(false);\n React.useLayoutEffect(() => {\n if (!element) return;\n const measure = () => setClamped(element.scrollHeight > element.clientHeight + 1);\n measure();\n const observer = new ResizeObserver(measure);\n observer.observe(element);\n return () => observer.disconnect();\n }, [element, lines, children]);\n return (\n \n \n {children}\n \n \n );\n}\nconst hasVisibleText = (root: HTMLElement) => {\n const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);\n for (let node = walker.nextNode(); node; node = walker.nextNode()) {\n if (!node.textContent?.trim()) continue;\n let element = node.parentElement;\n while (element && element !== root && element.offsetWidth > 1) element = element.parentElement;\n if (element === root) return true;\n }\n return false;\n};\n\nfunction useIconOnly(enabled: boolean) {\n const [element, setElement] = React.useState(null);\n const [iconOnly, setIconOnly] = React.useState(false);\n React.useLayoutEffect(() => {\n if (!enabled || !element) return setIconOnly(false);\n const sync = () => setIconOnly(!hasVisibleText(element));\n sync();\n const observer = new MutationObserver(sync);\n observer.observe(element, { childList: true, subtree: true, characterData: true });\n return () => observer.disconnect();\n }, [enabled, element]);\n return [iconOnly, setElement] as const;\n}\n\nfunction Item({\n className,\n variant = \"default\",\n size = \"default\",\n asChild = false,\n title,\n description,\n icon,\n media,\n badge,\n meta,\n error,\n actions,\n actionsPlacement = \"end\",\n unread,\n resolved,\n truncate,\n titleLines,\n mono = false,\n children,\n ref,\n ...props\n}: ItemProps & { ref?: React.Ref }) {\n const inList = React.useContext(ItemGroupContext);\n const Comp = asChild ? Slot.Root : \"div\";\n const itemRef = useDescendantFlags(itemFlags, undefined, ref);\n const composed = title !== undefined || media || icon || description || actions;\n const hasActions = Boolean(actions) && (size === \"default\" || size === \"md\");\n const truncated = truncate ?? !hasActions;\n const [actionsIconOnly, actionsRef] = useIconOnly(hasActions);\n const metaNode = meta && (\n {meta}\n );\n const body = composed ? (\n <>\n {unread && Unread}\n {(media || icon) && (\n \n {media ?? }\n {unread && }\n \n )}\n \n
\n
\n {unread && !(media || icon) && (\n \n )}\n {titleLines ? (\n \n {title}\n \n ) : (\n \n {title}\n \n )}\n {badge}\n {hasActions && metaNode}\n
\n {description && (\n \n {description}\n
\n )}\n {error && (\n
\n \n {error}\n
\n )}\n {children && !asChild &&
{children}
}\n \n {(actions || meta) && (\n \n {!hasActions && metaNode}\n {actions}\n \n )}\n \n \n ) : (\n children\n );\n const content =\n composed && asChild && React.isValidElement<{ children?: React.ReactNode }>(children)\n ? React.cloneElement(children, undefined, body)\n : body;\n const item = (\n \n {content}\n \n );\n return inList && asChild ?
{item}
: item;\n}\nItem.displayName = \"Item\";\n\nconst itemMediaVariants = cva(\n \"flex shrink-0 items-center justify-center gap-2 group-data-[has-description]/item:translate-y-0.5 group-data-[has-description]/item:self-start [&_svg]:pointer-events-none\",\n {\n variants: {\n variant: {\n default: \"bg-transparent\",\n icon: \"[&_svg:not([class*='size-'])]:size-4\",\n image:\n \"size-10 overflow-hidden rounded-sm group-data-[size=sm]/item:size-8 group-data-[size=xs]/item:size-6 [&_img]:size-full [&_img]:object-cover\"\n }\n },\n defaultVariants: {\n variant: \"default\"\n }\n }\n);\n\nfunction ItemMedia({\n className,\n variant = \"default\",\n ref,\n ...props\n}: React.ComponentPropsWithoutRef<\"div\"> &\n VariantProps & { ref?: React.Ref> }) {\n return (\n \n );\n}\nItemMedia.displayName = \"ItemMedia\";\n\nfunction ItemContent({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemContent.displayName = \"ItemContent\";\n\nfunction ItemTitle({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemTitle.displayName = \"ItemTitle\";\n\nfunction ItemDescription({ className, ref, ...props }: React.ComponentProps<\"p\">) {\n return (\n a:hover]:text-primary line-clamp-2 text-left [&>a]:underline [&>a]:underline-offset-4\",\n className\n )}\n {...props}\n />\n );\n}\nItemDescription.displayName = \"ItemDescription\";\n\nfunction ItemActions({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemActions.displayName = \"ItemActions\";\n\nfunction ItemHeader({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemHeader.displayName = \"ItemHeader\";\n\nfunction ItemFooter({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemFooter.displayName = \"ItemFooter\";\n\nexport {\n Item,\n ItemActions,\n ItemContent,\n ItemDescription,\n ItemFooter,\n ItemGroup,\n ItemHeader,\n ItemMedia,\n ItemSeparator,\n ItemTitle\n};\n\nexport function Media({\n icon,\n size = \"md\",\n tone,\n children,\n className,\n style,\n ref,\n ...props\n}: React.ComponentPropsWithoutRef<\"span\"> & {\n icon?: IconSource;\n size?: \"sm\" | \"md\" | number;\n tone?: \"ok\" | \"warn\" | \"bad\" | \"info\";\n} & { ref?: React.Ref }) {\n return (\n \n {icon ? (\n \n ) : (\n children\n )}\n
\n );\n}\nMedia.displayName = \"Media\";\nexport function Tile({\n selected,\n title,\n description,\n media,\n layout = \"row\",\n eyebrow,\n disabledReason,\n children,\n className,\n onClick,\n type = \"button\",\n ref,\n ...props\n}: Omit, \"title\"> & {\n selected?: boolean;\n title?: React.ReactNode;\n description?: React.ReactNode;\n media?: React.ReactNode;\n /** stacked puts the media and eyebrow on top, then the title on one line and a description clamped at two, with no chevron. For grids of choices such as a TilePicker. */\n layout?: \"row\" | \"stacked\";\n /** A short label across from the media in the stacked layout, such as a category. */\n eyebrow?: React.ReactNode;\n /** Disables the tile and shows why in place of the description. Unlike disabled, it stays focusable so the reason can be reached. */\n disabledReason?: string;\n} & { ref?: React.Ref }) {\n const inFrame = useInFrame(\"Tile\");\n const reason = disabledReason || undefined;\n const detail = reason ?? description;\n const stacked = layout === \"stacked\";\n return (\n {\n if (reason) {\n event.preventDefault();\n return;\n }\n onClick?.(event);\n }}\n {...props}\n >\n {stacked\n ? (media || eyebrow) && (\n \n {media && {media}}\n {eyebrow && (\n \n {eyebrow}\n \n )}\n \n )\n : media && {media}}\n \n {title !== undefined ? (\n <>\n \n {title}\n \n {detail &&\n (stacked ? (\n \n {detail}\n \n ) : (\n \n {detail}\n \n ))}\n {children}\n \n ) : (\n children\n )}\n \n {!stacked && }\n \n );\n}\nTile.displayName = \"Tile\";\n" + "content": "import * as React from \"react\";\nimport { cva, type VariantProps } from \"class-variance-authority\";\nimport { Slot } from \"radix-ui\";\n\nimport { Separator } from \"./separator\";\n\nimport { useDescendantFlags } from \"../lib/descendant-flags\";\nimport { useInFrame } from \"../lib/frame-context\";\nimport { cn } from \"../lib/utils\";\nimport { Icon, type IconSource } from \"./icon\";\nimport { Tooltip } from \"./tooltip\";\n\nimport controlStyles from \"./controls.module.css\";\nimport helperStyles from \"./helpers.module.css\";\nimport itemStyles from \"./item.module.css\";\n\nconst ItemGroupContext = React.createContext(false);\n\nconst groupFlags = { \"data-has-sm\": '[data-size=\"sm\"]', \"data-has-xs\": '[data-size=\"xs\"]' };\nconst groupWatch = { attributes: [\"data-size\"] };\nconst itemFlags = { \"data-has-description\": '[data-slot=\"item-description\"]' };\n\nfunction ItemGroup({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n const groupRef = useDescendantFlags(groupFlags, groupWatch, ref);\n return (\n \n \n \n );\n}\nItemGroup.displayName = \"ItemGroup\";\n\nfunction ItemSeparator({ className, ref, ...props }: React.ComponentProps) {\n return (\n \n );\n}\nItemSeparator.displayName = \"ItemSeparator\";\n\nconst itemVariants = cva(\n [\n controlStyles.focusOutline,\n \"group/item flex w-full flex-wrap rounded-lg text-sm transition-colors duration-(--cipher-duration-fast) [a]:transition-colors [a]:hover:bg-[color:var(--cipher-action-ghost-hover)]\"\n ],\n {\n variants: {\n variant: {\n default: \"border border-transparent\",\n outline: \"border border-[color:var(--cipher-line-soft)]\",\n muted: \"border border-transparent bg-muted/50\"\n },\n size: {\n default: \"gap-2.5 px-3 py-2.5\",\n md: \"gap-2.5 px-3 py-2.5\",\n sm: \"gap-2.5 px-3 py-2.5\",\n xs: \"gap-2 px-3 py-2 in-data-[slot=dropdown-menu-content]:p-0\"\n }\n },\n defaultVariants: {\n variant: \"default\",\n size: \"default\"\n }\n }\n);\n\nexport type ItemProps = Omit, \"title\"> &\n VariantProps & {\n asChild?: boolean;\n title?: React.ReactNode;\n description?: React.ReactNode;\n icon?: IconSource;\n media?: React.ReactNode;\n badge?: React.ReactNode;\n meta?: React.ReactNode;\n error?: React.ReactNode;\n /** Buttons at the row end. They sit beside the copy once the row is 36rem wide and stack below it under that. The width is the content box, so an outline Item needs its padding and border on top (about 39rem outside). */\n actions?: React.ReactNode;\n actionsPlacement?: \"end\" | \"below\";\n unread?: boolean;\n resolved?: boolean;\n /**\n * Keeps the title and description on one line with an ellipsis, and puts the full text in a\n * native tooltip. Unset, the title still truncates and the description wraps to two lines\n * before its ellipsis, except on a row with actions at the default or md size, where both\n * wrap. titleLines takes over the title when set, so truncate then governs only the\n * description.\n */\n truncate?: boolean;\n /**\n * Clamps the title to this many lines and shows the full title in a Tooltip while it is cut\n * off. Unset leaves the title to truncate. titleLines={1} reads like truncate but uses a Cipher\n * tooltip instead of a native one.\n */\n titleLines?: number;\n mono?: boolean;\n };\n\nfunction ClampedTitle({\n lines,\n className,\n children\n}: {\n lines: number;\n className?: string;\n children: React.ReactNode;\n}) {\n const [element, setElement] = React.useState(null);\n const [clamped, setClamped] = React.useState(false);\n React.useLayoutEffect(() => {\n if (!element) return;\n const measure = () => setClamped(element.scrollHeight > element.clientHeight + 1);\n measure();\n const observer = new ResizeObserver(measure);\n observer.observe(element);\n return () => observer.disconnect();\n }, [element, lines, children]);\n return (\n \n \n {children}\n \n \n );\n}\nconst hasVisibleText = (root: HTMLElement) => {\n const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);\n for (let node = walker.nextNode(); node; node = walker.nextNode()) {\n if (!node.textContent?.trim()) continue;\n let element = node.parentElement;\n while (\n element &&\n element !== root &&\n (element.offsetWidth > 1 || getComputedStyle(element).display === \"contents\")\n )\n element = element.parentElement;\n if (element === root) return true;\n }\n return false;\n};\n\nfunction useIconOnly(enabled: boolean) {\n const [element, setElement] = React.useState(null);\n const [iconOnly, setIconOnly] = React.useState(false);\n React.useLayoutEffect(() => {\n if (!enabled || !element) return setIconOnly(false);\n const sync = () => setIconOnly(!hasVisibleText(element));\n sync();\n const observer = new MutationObserver(sync);\n observer.observe(element, { childList: true, subtree: true, characterData: true });\n return () => observer.disconnect();\n }, [enabled, element]);\n return [iconOnly, setElement] as const;\n}\n\nfunction Item({\n className,\n variant = \"default\",\n size = \"default\",\n asChild = false,\n title,\n description,\n icon,\n media,\n badge,\n meta,\n error,\n actions,\n actionsPlacement = \"end\",\n unread,\n resolved,\n truncate,\n titleLines,\n mono = false,\n children,\n ref,\n ...props\n}: ItemProps & { ref?: React.Ref }) {\n const inList = React.useContext(ItemGroupContext);\n const Comp = asChild ? Slot.Root : \"div\";\n const itemRef = useDescendantFlags(itemFlags, undefined, ref);\n const composed = title !== undefined || media || icon || description || actions;\n const hasActions = Boolean(actions) && (size === \"default\" || size === \"md\");\n const truncated = truncate ?? !hasActions;\n const [actionsIconOnly, actionsRef] = useIconOnly(hasActions);\n const metaNode = meta && (\n {meta}\n );\n const body = composed ? (\n <>\n {unread && Unread}\n {(media || icon) && (\n \n {media ?? }\n {unread && }\n \n )}\n \n
\n
\n {unread && !(media || icon) && (\n \n )}\n {titleLines ? (\n \n {title}\n \n ) : (\n \n {title}\n \n )}\n {badge}\n {hasActions && metaNode}\n
\n {description && (\n \n {description}\n
\n )}\n {error && (\n
\n \n {error}\n
\n )}\n {children && !asChild &&
{children}
}\n \n {(actions || meta) && (\n \n {!hasActions && metaNode}\n {actions}\n \n )}\n \n \n ) : (\n children\n );\n const content =\n composed && asChild && React.isValidElement<{ children?: React.ReactNode }>(children)\n ? React.cloneElement(children, undefined, body)\n : body;\n const item = (\n \n {content}\n \n );\n return inList && asChild ?
{item}
: item;\n}\nItem.displayName = \"Item\";\n\nconst itemMediaVariants = cva(\n \"flex shrink-0 items-center justify-center gap-2 group-data-[has-description]/item:translate-y-0.5 group-data-[has-description]/item:self-start [&_svg]:pointer-events-none\",\n {\n variants: {\n variant: {\n default: \"bg-transparent\",\n icon: \"[&_svg:not([class*='size-'])]:size-4\",\n image:\n \"size-10 overflow-hidden rounded-sm group-data-[size=sm]/item:size-8 group-data-[size=xs]/item:size-6 [&_img]:size-full [&_img]:object-cover\"\n }\n },\n defaultVariants: {\n variant: \"default\"\n }\n }\n);\n\nfunction ItemMedia({\n className,\n variant = \"default\",\n ref,\n ...props\n}: React.ComponentPropsWithoutRef<\"div\"> &\n VariantProps & { ref?: React.Ref> }) {\n return (\n \n );\n}\nItemMedia.displayName = \"ItemMedia\";\n\nfunction ItemContent({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemContent.displayName = \"ItemContent\";\n\nfunction ItemTitle({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemTitle.displayName = \"ItemTitle\";\n\nfunction ItemDescription({ className, ref, ...props }: React.ComponentProps<\"p\">) {\n return (\n a:hover]:text-primary line-clamp-2 text-left [&>a]:underline [&>a]:underline-offset-4\",\n className\n )}\n {...props}\n />\n );\n}\nItemDescription.displayName = \"ItemDescription\";\n\nfunction ItemActions({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemActions.displayName = \"ItemActions\";\n\nfunction ItemHeader({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemHeader.displayName = \"ItemHeader\";\n\nfunction ItemFooter({ className, ref, ...props }: React.ComponentProps<\"div\">) {\n return (\n \n );\n}\nItemFooter.displayName = \"ItemFooter\";\n\nexport {\n Item,\n ItemActions,\n ItemContent,\n ItemDescription,\n ItemFooter,\n ItemGroup,\n ItemHeader,\n ItemMedia,\n ItemSeparator,\n ItemTitle\n};\n\nexport function Media({\n icon,\n size = \"md\",\n tone,\n children,\n className,\n style,\n ref,\n ...props\n}: React.ComponentPropsWithoutRef<\"span\"> & {\n icon?: IconSource;\n size?: \"sm\" | \"md\" | number;\n tone?: \"ok\" | \"warn\" | \"bad\" | \"info\";\n} & { ref?: React.Ref }) {\n return (\n \n {icon ? (\n \n ) : (\n children\n )}\n
\n );\n}\nMedia.displayName = \"Media\";\nexport function Tile({\n selected,\n title,\n description,\n media,\n layout = \"row\",\n eyebrow,\n disabledReason,\n children,\n className,\n onClick,\n type = \"button\",\n ref,\n ...props\n}: Omit, \"title\"> & {\n selected?: boolean;\n title?: React.ReactNode;\n description?: React.ReactNode;\n media?: React.ReactNode;\n /** stacked puts the media and eyebrow on top, then the title on one line and a description clamped at two, with no chevron. For grids of choices such as a TilePicker. */\n layout?: \"row\" | \"stacked\";\n /** A short label across from the media in the stacked layout, such as a category. */\n eyebrow?: React.ReactNode;\n /** Disables the tile and shows why in place of the description. Unlike disabled, it stays focusable so the reason can be reached. */\n disabledReason?: string;\n} & { ref?: React.Ref }) {\n const inFrame = useInFrame(\"Tile\");\n const reason = disabledReason || undefined;\n const detail = reason ?? description;\n const stacked = layout === \"stacked\";\n return (\n {\n if (reason) {\n event.preventDefault();\n return;\n }\n onClick?.(event);\n }}\n {...props}\n >\n {stacked\n ? (media || eyebrow) && (\n \n {media && {media}}\n {eyebrow && (\n \n {eyebrow}\n \n )}\n \n )\n : media && {media}}\n \n {title !== undefined ? (\n <>\n \n {title}\n \n {detail &&\n (stacked ? (\n \n {detail}\n \n ) : (\n \n {detail}\n \n ))}\n {children}\n \n ) : (\n children\n )}\n \n {!stacked && }\n \n );\n}\nTile.displayName = \"Tile\";\n" }, { "path": "src/components/cipher/ui/controls.module.css", diff --git a/src/ui/item.tsx b/src/ui/item.tsx index 4a6fdd0e..25e0de27 100644 --- a/src/ui/item.tsx +++ b/src/ui/item.tsx @@ -142,7 +142,12 @@ const hasVisibleText = (root: HTMLElement) => { for (let node = walker.nextNode(); node; node = walker.nextNode()) { if (!node.textContent?.trim()) continue; let element = node.parentElement; - while (element && element !== root && element.offsetWidth > 1) element = element.parentElement; + while ( + element && + element !== root && + (element.offsetWidth > 1 || getComputedStyle(element).display === "contents") + ) + element = element.parentElement; if (element === root) return true; } return false;