diff --git a/.changeset/verse-action-label-wrap.md b/.changeset/verse-action-label-wrap.md new file mode 100644 index 000000000..8c449877d --- /dev/null +++ b/.changeset/verse-action-label-wrap.md @@ -0,0 +1,5 @@ +--- +'@youversion/platform-react-ui': patch +--- + +Long custom verse action labels now wrap to two lines. Each button fits its text, and icons, swatches, and the divider stay on one line. diff --git a/packages/ui/src/components/verse-action-popover.stories.tsx b/packages/ui/src/components/verse-action-popover.stories.tsx new file mode 100644 index 000000000..eb745345f --- /dev/null +++ b/packages/ui/src/components/verse-action-popover.stories.tsx @@ -0,0 +1,155 @@ +import type { Meta, StoryObj } from '@storybook/react-vite'; + +import { useState } from 'react'; +import { expect, within } from 'storybook/test'; +import { waitFor } from '../test/storybook-dom'; +import { BookOpenIcon } from './icons/book-open'; +import { GlobeIcon } from './icons/globe'; +import { FlameIcon } from './icons/flame'; +import { ClockBackIcon } from './icons/clock-back'; +import { VerseActionPopover, type VerseActionPopoverItem } from './verse-action-popover'; + +const icon = (Icon: typeof BookOpenIcon) => ; + +/** + * Host labels arrive already localized, so a bar can mix short built-ins with + * long translated or descriptive custom actions. Each item stresses one case. + */ +const DATASETS = { + /** What most apps show: no `actionItems`. */ + default: undefined, + demo: [ + 'highlights', + 'copy', + 'share', + { id: 'custom', label: 'Custom Action', icon: icon(FlameIcon), onSelect: () => {} }, + ], + worstCase: [ + 'highlights', + 'copy', + // German compound words have no break opportunity. + { + id: 'prayer-list', + label: 'Zur Gebetsanliegenliste hinzufügen', + icon: icon(BookOpenIcon), + onSelect: () => {}, + }, + 'share', + // Stacked Vietnamese diacritics are clipped by a tight line box. + { id: 'plan', label: 'Thêm vào kế hoạch đọc', icon: icon(ClockBackIcon), onSelect: () => {} }, + // A descriptive English action longer than two short lines. + { + id: 'discussion', + label: 'Add these verses to tomorrow’s small-group discussion', + icon: icon(GlobeIcon), + onSelect: () => {}, + }, + { id: 'pin', label: 'Pin', icon: icon(FlameIcon), onSelect: () => {} }, + ], +} satisfies Record; + +type VerseActionLabelsProps = { + data: keyof typeof DATASETS; + theme?: 'light' | 'dark'; +}; + +function VerseActionLabels({ data, theme }: VerseActionLabelsProps): React.ReactNode { + const [anchor, setAnchor] = useState(null); + return ( +
+ + In the beginning was the Word. + + {}} + activeHighlights={new Set()} + selectedVerses={[1]} + highlightedVerses={{}} + anchorElement={anchor} + actionItems={DATASETS[data]} + includeSave + onHighlight={() => {}} + onClearHighlight={() => {}} + onSave={() => {}} + onCopy={() => {}} + onShare={() => {}} + theme={theme} + /> +
+ ); +} + +const meta = { + title: 'Components/VerseActionPopover', + component: VerseActionLabels, + parameters: { layout: 'centered' }, + argTypes: { + data: { + control: 'inline-radio', + options: Object.keys(DATASETS), + description: 'Action items passed through `actionItems`.', + }, + }, + render: (args, { globals }) => ( + + ), +} satisfies Meta; + +export default meta; +type Story = StoryObj; + +/** Built-in actions only, as most apps show the bar. */ +export const Default: Story = { args: { data: 'default' } }; + +/** Long, translated, and very short host labels side by side. */ +export const LongCustomLabels: Story = { + tags: ['integration'], + args: { data: 'worstCase' }, + play: async ({ canvasElement }) => { + const dialog = await within(canvasElement.ownerDocument.body).findByRole('dialog', { + name: 'Verse actions', + }); + await Promise.all(dialog.getAnimations().map((animation) => animation.finished)); + const actions = within(dialog); + const centerY = (element: Element) => { + const { top, height } = element.getBoundingClientRect(); + return top + height / 2; + }; + const copy = actions.getByRole('button', { name: 'Copy' }); + const discussion = actions.getByRole('button', { + name: 'Add these verses to tomorrow’s small-group discussion', + }); + const label = discussion.lastElementChild; + if (!copy.firstElementChild || !label) throw new Error('Action button parts missing'); + const lineHeight = Number.parseFloat(getComputedStyle(label).lineHeight); + + await waitFor(async () => { + // The long label wraps to two clamped lines instead of widening the bar. + await expect(label.getBoundingClientRect().height).toBe(lineHeight * 2); + // Its box hugs its longest visible line, so the gaps beside it match the others. + const range = label.ownerDocument.createRange(); + range.selectNodeContents(label); + const visibleLines = Array.from(range.getClientRects()).filter( + (line) => line.top < label.getBoundingClientRect().bottom, + ); + const widest = Math.max(...visibleLines.map((line) => line.width)); + await expect(label.getBoundingClientRect().width - widest).toBeLessThan(1); + }); + // A taller button grows downward: icons stay level, and the swatches and divider + // stay centered on a one-line button. + for (const button of actions.getAllByRole('button').filter((b) => !b.closest('[role=group]'))) { + if (!button.firstElementChild) throw new Error('Action icon missing'); + await expect(centerY(button.firstElementChild)).toBeCloseTo( + centerY(copy.firstElementChild), + 0, + ); + } + await expect(centerY(actions.getByRole('group', { name: 'Highlight colors' }))).toBeCloseTo( + centerY(copy), + 0, + ); + await expect(discussion).toHaveAttribute('title', discussion.getAttribute('aria-label')); + await expect(copy).not.toHaveAttribute('title'); + }, +}; diff --git a/packages/ui/src/components/verse-action-popover.tsx b/packages/ui/src/components/verse-action-popover.tsx index aa6497ebd..41d021812 100644 --- a/packages/ui/src/components/verse-action-popover.tsx +++ b/packages/ui/src/components/verse-action-popover.tsx @@ -201,20 +201,55 @@ function highlightSwatchLabel(t: TFunction, color: string, showRemove: boolean): type ActionButtonProps = { icon: ReactNode; label: string; + /** Full text on hover, for host labels that may be clamped. */ + title?: string; onClick: () => void; onClickCapture?: () => void; }; -function ActionButton({ icon, label, onClick, onClickCapture }: ActionButtonProps) { +function ActionButton({ icon, label, title, onClick, onClickCapture }: ActionButtonProps) { + // CSS sizes a wrapped label to the 8rem cap rather than to its longest line, which + // leaves wider gaps beside it than between one-line actions. Shrink the label to its + // longest line, in em so zoom and text size keep the fit. + const labelRef = useRef(null); + useLayoutEffect(() => { + const element = labelRef.current; + // Skip an element without layout, such as one rendered in jsdom. + if (!element?.offsetWidth) return; + const fit = (): void => { + element.style.inlineSize = ''; + const range = element.ownerDocument.createRange(); + range.selectNodeContents(element); + // Lines hidden by the two-line clamp still report rects; skip them. Rects include + // the bar's zoom-in transform, so divide it out. + const { bottom, width } = element.getBoundingClientRect(); + const lines = Array.from(range.getClientRects()) + .filter((line) => line.top < bottom) + .map((line) => line.width); + if (lines.length < 2) return; + const widest = (Math.max(...lines) * element.offsetWidth) / width; + const fontSize = Number.parseFloat(getComputedStyle(element).fontSize); + element.style.inlineSize = `${Math.ceil(widest) / fontSize}em`; + }; + fit(); + // A web font that finishes loading changes the line widths. + const { fonts } = element.ownerDocument; + fonts.addEventListener('loadingdone', fit); + return () => fonts.removeEventListener('loadingdone', fit); + }, [label]); + return ( ); } @@ -572,7 +614,7 @@ export const VerseActionPopover: FC = ({ 'yv:bg-card yv:text-popover-foreground', 'yv:rounded-full yv:drop-shadow-[0px_4.8432px_20px_rgba(0,0,0,0.19)]', 'yv:px-4 yv:py-2', - 'yv:flex yv:items-center yv:gap-1', + 'yv:flex yv:items-start yv:gap-1', // Never wider than the viewport (minus collision padding). When the // swatch row can't fit, it scrolls inside instead of overflowing the // screen. Radix sets `--radix-popover-content-available-width`, but @@ -614,7 +656,7 @@ export const VerseActionPopover: FC = ({ inert={!portal.open} className={ view.scrollAllActions - ? 'yv:flex yv:min-w-0 yv:items-center yv:gap-1 yv:overflow-x-auto yv:p-1 yv:-m-1' + ? 'yv:flex yv:min-w-0 yv:items-start yv:gap-1 yv:overflow-x-auto yv:p-1 yv:-m-1' : 'yv:contents' } > @@ -652,6 +694,7 @@ export const VerseActionPopover: FC = ({ key={`custom:${item.id}`} icon={item.icon} label={item.label} + title={item.label} onClick={item.onSelect} onClickCapture={markOverlayAction} /> @@ -659,9 +702,12 @@ export const VerseActionPopover: FC = ({ } if (!highlightsEnabled) return null; + // The row top-aligns, so the divider and swatches center on a + // one-line action button (3rem) and keep their place when a host + // label wraps. const divider = (