Repository navigation
WIP: fix(ui): wrap long verse action labels without breaking bar alignment #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cameronapak
wants to merge
1
commit into
main
Choose a base branch
from
fix/verse-action-label-wrap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
155 changes: 155 additions & 0 deletions
155
packages/ui/src/components/verse-action-popover.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) => <Icon className="yv:size-5" />; | ||
|
|
||
| /** | ||
| * 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<string, readonly VerseActionPopoverItem[] | undefined>; | ||
|
|
||
| type VerseActionLabelsProps = { | ||
| data: keyof typeof DATASETS; | ||
| theme?: 'light' | 'dark'; | ||
| }; | ||
|
|
||
| function VerseActionLabels({ data, theme }: VerseActionLabelsProps): React.ReactNode { | ||
| const [anchor, setAnchor] = useState<HTMLElement | null>(null); | ||
| return ( | ||
| <div className="yv:pb-48 yv:text-center"> | ||
| <span ref={setAnchor} className="yv-v" style={{ color: 'gray' }}> | ||
| In the beginning was the Word. | ||
| </span> | ||
| <VerseActionPopover | ||
| open={anchor !== null} | ||
| onOpenChange={() => {}} | ||
| activeHighlights={new Set()} | ||
| selectedVerses={[1]} | ||
| highlightedVerses={{}} | ||
| anchorElement={anchor} | ||
| actionItems={DATASETS[data]} | ||
| includeSave | ||
| onHighlight={() => {}} | ||
| onClearHighlight={() => {}} | ||
| onSave={() => {}} | ||
| onCopy={() => {}} | ||
| onShare={() => {}} | ||
| theme={theme} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| 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 }) => ( | ||
| <VerseActionLabels {...args} theme={globals.theme === 'dark' ? 'dark' : 'light'} /> | ||
| ), | ||
| } satisfies Meta<typeof VerseActionLabels>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| /** 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); | ||
|
Check failure on line 137 in packages/ui/src/components/verse-action-popover.stories.tsx
|
||
| }); | ||
| // 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'); | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Range.getClientRects()can return several text fragments for one line when a custom label mixes Arabic or Hebrew with Latin text. Taking the widest fragment makesinlineSizesmaller than the complete line. Even a short label such as “حفظ في Notes” can then wrap unnecessarily or lose text to the two-line clamp.Group fragments by their vertical position and measure each complete line before shrinking the label. Add a mixed-direction label to the story.
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!