-
Notifications
You must be signed in to change notification settings - Fork 3.8k
refactor(ui): centralize overlay action buttons in EMCN #7990
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
Open
BillLeoutsakosvl346
wants to merge
1
commit into
codex/control-fix-field-disclosures
Choose a base branch
from
codex/control-fix-overlay-action-buttons
base: codex/control-fix-field-disclosures
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.
Open
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
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
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
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
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
117 changes: 117 additions & 0 deletions
117
packages/emcn/src/components/overlay-action-button/overlay-action-button.test.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,117 @@ | ||
| /** @vitest-environment jsdom */ | ||
| import { act, createRef, type ReactNode } from 'react' | ||
| import { Button, OverlayActionButton, Tooltip } from '@sim/emcn' | ||
| import { createRoot, type Root } from 'react-dom/client' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| let root: Root | null = null | ||
| let container: HTMLDivElement | null = null | ||
|
|
||
| function mount(children: ReactNode) { | ||
| ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true | ||
| container = document.createElement('div') | ||
| document.body.appendChild(container) | ||
| root = createRoot(container) | ||
| act(() => root?.render(children)) | ||
| return container | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| if (root) act(() => root?.unmount()) | ||
| container?.remove() | ||
| root = null | ||
| container = null | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| /** Pre-migration recipes from log details and the deployment preview. */ | ||
| const PREVIOUS = [ | ||
| { | ||
| name: 'default 20px adaptive action', | ||
| props: {}, | ||
| variant: 'default', | ||
| className: | ||
| 'size-[20px] cursor-pointer border-[var(--border-1)] bg-transparent p-0 backdrop-blur-xs hover-hover:bg-[var(--surface-3)]', | ||
| }, | ||
| { | ||
| name: '28px adaptive action', | ||
| props: { size: 'md' }, | ||
| variant: 'default', | ||
| className: | ||
| 'size-[28px] cursor-pointer bg-transparent p-0 backdrop-blur-xs hover-hover:bg-[var(--surface-3)]', | ||
| }, | ||
| ] as const | ||
|
|
||
| describe('OverlayActionButton', () => { | ||
| it.each(PREVIOUS)('preserves the previous $name markup', ({ props, variant, className }) => { | ||
| const view = mount( | ||
| <> | ||
| <Button variant={variant} aria-label='Copy' className={`${className} shrink-0`}> | ||
| <svg className='size-[10px]' aria-hidden='true' /> | ||
| </Button> | ||
| <OverlayActionButton {...props} aria-label='Copy' className='shrink-0'> | ||
| <svg className='size-[10px]' aria-hidden='true' /> | ||
| </OverlayActionButton> | ||
| </> | ||
| ) | ||
| const [previous, current] = view.querySelectorAll('button') | ||
| /** The old border-1 token aliases border; class order changes when recipes are composed. */ | ||
| for (const button of [previous, current]) { | ||
| button.className = button.className | ||
| .replaceAll('--border-1', '--border') | ||
| .split(/\s+/) | ||
| .sort() | ||
| .join(' ') | ||
| } | ||
| expect(current.outerHTML).toBe(previous.outerHTML) | ||
| }) | ||
|
|
||
| it('forwards refs and native props through a tooltip and suppresses disabled clicks', () => { | ||
| vi.useFakeTimers() | ||
| const ref = createRef<HTMLButtonElement>() | ||
| const onClick = vi.fn() | ||
| const onKeyDown = vi.fn() | ||
| const action = (disabled: boolean) => ( | ||
| <Tooltip.Root> | ||
| <Tooltip.Trigger asChild> | ||
| <OverlayActionButton | ||
| ref={ref} | ||
| type='button' | ||
| aria-label='Copy' | ||
| data-action='copy' | ||
| disabled={disabled} | ||
| onClick={onClick} | ||
| onKeyDown={onKeyDown} | ||
| /> | ||
| </Tooltip.Trigger> | ||
| <Tooltip.Content>Copy output</Tooltip.Content> | ||
| </Tooltip.Root> | ||
| ) | ||
| const view = mount(action(false)) | ||
| const button = view.querySelector('button') | ||
| if (!button) throw new Error('Button did not render') | ||
| expect(view.querySelectorAll('button')).toHaveLength(1) | ||
| expect(ref.current).toBe(button) | ||
| expect(button.type).toBe('button') | ||
| expect(button.dataset.action).toBe('copy') | ||
| expect(button.getAttribute('aria-label')).toBe('Copy') | ||
| act(() => | ||
| button.dispatchEvent( | ||
| new MouseEvent('pointerover', { bubbles: true, clientX: 200, clientY: 200 }) | ||
| ) | ||
| ) | ||
| expect(document.querySelector('[role="tooltip"]')?.textContent).toBe('Copy output') | ||
| act(() => button.focus()) | ||
| expect(document.activeElement).toBe(button) | ||
| const keyEvent = new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }) | ||
| act(() => button.dispatchEvent(keyEvent)) | ||
| expect(onKeyDown).toHaveBeenCalledTimes(1) | ||
| expect(onKeyDown.mock.calls[0][0].nativeEvent).toBe(keyEvent) | ||
| act(() => button.click()) | ||
| expect(onClick).toHaveBeenCalledTimes(1) | ||
| act(() => root?.render(action(true))) | ||
| expect(button.disabled).toBe(true) | ||
| act(() => button.click()) | ||
| expect(onClick).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.