This repository was archived by the owner on Oct 14, 2025. It is now read-only.
Repository navigation
Added useRole #45
Merged
Merged
Added useRole #45
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c2a54b7
Added useRole
Hugos68 522ecea
Added docs
Hugos68 fe5fbb0
space
Hugos68 d220e13
Merge branch 'dev' into feat/use-role
Hugos68 a091037
Started tests
Hugos68 ff42483
Added tests
Hugos68 d312d5e
skip failing test for now
Hugos68 510ed7d
Added tests
Hugos68 02ce0fa
Feedback
Hugos68 d63b7e8
Merge branch 'dev' into feat/use-role
Hugos68 4ef69f0
Merged dev, added changeset
Hugos68 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <script lang="ts"> | ||
| import { autoUpdate } from '@floating-ui/dom'; | ||
| import { useFloating } from '../useFloating/index.svelte.js'; | ||
| import { useInteractions } from '../useInteractions/index.svelte.js'; | ||
| import { useRole, type UseRoleOptions } from '../useRole/index.svelte.js'; | ||
| interface Props extends UseRoleOptions { | ||
| open?: boolean; | ||
| } | ||
| let { open = false, ...rest }: Props = $props(); | ||
| const elements: { reference: HTMLElement | null; floating: HTMLElement | null } = $state({ | ||
| reference: null, | ||
| floating: null | ||
| }); | ||
| const floating = useFloating({ | ||
| whileElementsMounted: autoUpdate, | ||
| get open() { | ||
| return open; | ||
| }, | ||
| onOpenChange(v) { | ||
| open = v; | ||
| }, | ||
| elements | ||
| }); | ||
| const role = useRole(floating.context, { ...rest }); | ||
| const interactions = useInteractions([role]); | ||
| </script> | ||
|
|
||
| <p>{open}</p> | ||
| <button bind:this={elements.reference} {...interactions.getReferenceProps()}> Reference </button> | ||
| {#if open} | ||
| <div | ||
| bind:this={elements.floating} | ||
| style={floating.floatingStyles} | ||
| {...interactions.getFloatingProps()} | ||
| > | ||
| Floating | ||
| </div> | ||
| {/if} |
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,111 @@ | ||
| import { Map as ReactiveMap } from 'svelte/reactivity'; | ||
| import type { FloatingContext } from '../useFloating/index.svelte.js'; | ||
| import type { ElementProps } from '../useInteractions/index.svelte.js'; | ||
|
|
||
| type AriaRole = 'tooltip' | 'dialog' | 'alertdialog' | 'menu' | 'listbox' | 'grid' | 'tree'; | ||
| type ComponentRole = 'select' | 'label' | 'combobox'; | ||
|
|
||
| interface UseRoleOptions { | ||
| /** | ||
| * Whether the Hook is enabled, including all internal Effects and event | ||
| * handlers. | ||
| * @default true | ||
| */ | ||
| enabled?: boolean; | ||
| /** | ||
| * The role of the floating element. | ||
| * @default 'dialog' | ||
| */ | ||
| role?: AriaRole | ComponentRole; | ||
| } | ||
|
|
||
| const componentRoleToAriaRoleMap = new ReactiveMap<AriaRole | ComponentRole, AriaRole | false>([ | ||
| ['select', 'listbox'], | ||
| ['combobox', 'listbox'], | ||
| ['label', false] | ||
| ]); | ||
|
|
||
| function useRole(context: FloatingContext, options: UseRoleOptions = {}): ElementProps { | ||
| const enabled = $derived(options.enabled ?? true); | ||
| const role = $derived(options.role ?? 'dialog'); | ||
|
|
||
| const ariaRole = $derived( | ||
| (componentRoleToAriaRoleMap.get(role) ?? role) as AriaRole | false | undefined | ||
| ); | ||
|
|
||
| // FIXME: Uncomment the commented code once useId and useFloatingParentNodeId are implemented. | ||
| const referenceId = '123abc'; | ||
| const parentId = undefined; | ||
| // const referenceId = useId(); | ||
| // const parentId = useFloatingParentNodeId(); | ||
|
|
||
| const isNested = parentId != null; | ||
|
|
||
| const elementProps: ElementProps = $derived.by(() => { | ||
| if (!enabled) { | ||
| return {}; | ||
| } | ||
|
|
||
| const floatingProps = { | ||
| id: context.floatingId, | ||
| ...(ariaRole && { role: ariaRole }) | ||
| }; | ||
|
|
||
| if (ariaRole === 'tooltip' || role === 'label') { | ||
| return { | ||
| reference: { | ||
| [`aria-${role === 'label' ? 'labelledby' : 'describedby'}`]: context.open | ||
| ? context.floatingId | ||
| : undefined | ||
| }, | ||
| floating: floatingProps | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| reference: { | ||
| 'aria-expanded': context.open ? 'true' : 'false', | ||
| 'aria-haspopup': ariaRole === 'alertdialog' ? 'dialog' : ariaRole, | ||
| 'aria-controls': context.open ? context.floatingId : undefined, | ||
| ...(ariaRole === 'listbox' && { role: 'combobox' }), | ||
| ...(ariaRole === 'menu' && { id: referenceId }), | ||
| ...(ariaRole === 'menu' && isNested && { role: 'menuitem' }), | ||
| ...(role === 'select' && { 'aria-autocomplete': 'none' }), | ||
| ...(role === 'combobox' && { 'aria-autocomplete': 'list' }) | ||
| }, | ||
| floating: { | ||
| ...floatingProps, | ||
| ...(ariaRole === 'menu' && { 'aria-labelledby': referenceId }) | ||
| }, | ||
| item({ active, selected }) { | ||
| const commonProps = { | ||
| role: 'option', | ||
| ...(active && { id: `${context.floatingId}-option` }) | ||
| }; | ||
|
|
||
| // For `menu`, we are unable to tell if the item is a `menuitemradio` | ||
| // or `menuitemcheckbox`. For backwards-compatibility reasons, also | ||
| // avoid defaulting to `menuitem` as it may overwrite custom role props. | ||
| switch (role) { | ||
| case 'select': | ||
| return { | ||
| ...commonProps, | ||
| 'aria-selected': active && selected | ||
| }; | ||
| case 'combobox': { | ||
| return { | ||
| ...commonProps, | ||
| ...(active && { 'aria-selected': true }) | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return {}; | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| return elementProps; | ||
| } | ||
|
|
||
| export { useRole, type UseRoleOptions }; |
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,72 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { cleanup, render, screen } from '@testing-library/svelte'; | ||
| import App from './App.test.svelte'; | ||
|
|
||
| const ARIA_ROLES = ['grid', 'listbox', 'menu', 'tree', 'tooltip', 'alertdialog', 'dialog'] as const; | ||
|
|
||
| describe('useRole', () => { | ||
| it('by default applies the "dialog" role to the floating element', () => { | ||
| render(App, { role: undefined, open: true }); | ||
| expect(screen.queryByRole('dialog')).toBeInTheDocument(); | ||
| cleanup(); | ||
| }); | ||
|
|
||
| for (const role of ARIA_ROLES) { | ||
| it(`applies the "${role}" role to the floating element`, () => { | ||
| render(App, { role, open: true }); | ||
| expect(screen.queryByRole(role)).toBeInTheDocument(); | ||
| cleanup(); | ||
| }); | ||
| } | ||
|
|
||
| describe('tooltip', () => { | ||
| it.skip('sets correct aria attributes based on the open state', async () => { | ||
| const { rerender } = render(App, { role: 'tooltip', open: true }); | ||
|
|
||
| expect(screen.getByRole('button')).toHaveAttribute( | ||
| 'aria-describedby', | ||
| screen.getByRole('tooltip').getAttribute('id') | ||
| ); | ||
|
|
||
| await rerender({ role: 'tooltip', open: false }); | ||
|
|
||
| expect(screen.getByRole('buton')).not.toHaveAttribute('aria-describedby'); | ||
|
|
||
| cleanup(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('label', () => { | ||
| it.skip('sets correct aria attributes based on the open state', async () => { | ||
| const { rerender } = render(App, { role: 'label', open: true }); | ||
|
|
||
| expect(screen.getByRole('button')).toHaveAttribute( | ||
| 'aria-labelledby', | ||
| screen.getByRole('tooltip').getAttribute('id') | ||
| ); | ||
|
|
||
| await rerender({ role: 'tooltip', open: false }); | ||
|
|
||
| expect(screen.getByRole('buton')).not.toHaveAttribute('aria-labelledby'); | ||
|
|
||
| cleanup(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('dialog', () => { | ||
| it.skip('sets correct aria attributes based on the open state', async () => { | ||
| const { rerender } = render(App, { role: 'dialog', open: false }); | ||
|
|
||
| expect(screen.getByRole('button')).toHaveAttribute('aria-haspopup', 'dialog'); | ||
| expect(screen.getByRole('button')).toHaveAttribute('aria-expanded', 'false'); | ||
|
|
||
| await rerender({ role: 'dialog', open: true }); | ||
|
|
||
| expect(screen.getByRole('dialog')).toBeInTheDocument(); | ||
| expect(screen.getByRole('button')).toHaveAttribute( | ||
| 'aria-controls', | ||
| screen.getByRole('dialog').getAttribute('id') | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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.