-
Notifications
You must be signed in to change notification settings - Fork 76
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
fix: Disable combobox portal inside dialog #14661
base: main
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughThis pull request introduces changes to the StudioCombobox component and its supporting files. It adds a new Storybook story, Changes
Suggested labels
Suggested reviewers
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 ESLint
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsxOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-storybook". (The package "eslint-plugin-storybook" was not found when loaded as a Node module from the directory "/frontend/libs/studio-components".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "eslint-plugin-storybook" was referenced from the config file in "frontend/libs/studio-components/.eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsxOops! Something went wrong! :( ESLint: 8.57.1 ESLint couldn't find the plugin "eslint-plugin-storybook". (The package "eslint-plugin-storybook" was not found when loaded as a Node module from the directory "/frontend/libs/studio-components".) It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:
The plugin "eslint-plugin-storybook" was referenced from the config file in "frontend/libs/studio-components/.eslintrc.js". If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team. Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #14661 +/- ##
=======================================
Coverage 95.75% 95.76%
=======================================
Files 1913 1914 +1
Lines 24925 24936 +11
Branches 2848 2849 +1
=======================================
+ Hits 23868 23879 +11
Misses 799 799
Partials 258 258 ☔ View full report in Codecov by Sentry. |
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
frontend/libs/studio-components/src/components/StudioCombobox/isWithinDialog.test.tsx (1)
5-21
: Consider adding edge case tests.The current test coverage is good but could be enhanced with additional edge cases:
- Nested dialogs
- Closed dialog
- Null/undefined element
Here's an example of additional test cases:
describe('isWithinDialog', () => { it('Returns true when the element is inside a dialog', () => { render( <dialog open> <button /> </dialog>, ); const button = screen.getByRole('button'); expect(isWithinDialog(button)).toBe(true); }); it('Returns false when the element is not inside a dialog', () => { render(<button />); const button = screen.getByRole('button'); expect(isWithinDialog(button)).toBe(false); }); + + it('Returns true for element in nested dialogs', () => { + render( + <dialog open> + <dialog open> + <button /> + </dialog> + </dialog>, + ); + const button = screen.getByRole('button'); + expect(isWithinDialog(button)).toBe(true); + }); + + it('Returns true for element in closed dialog', () => { + render( + <dialog> + <button /> + </dialog>, + ); + const button = screen.getByRole('button'); + expect(isWithinDialog(button)).toBe(true); + }); });frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsx (1)
16-22
: Consider memoizing the callback with useCallback.The callback is already memoized with useCallback, but it could be optimized further by moving the dialog check logic outside the callback.
Here's a suggested optimization:
- const internalRef = useCallback( - (node?: HTMLInputElement) => { - forwardedRef.current = node; - if (node && isWithinDialog(node)) setPortal(false); - }, - [forwardedRef], - ); + const checkDialog = useCallback( + (node?: HTMLInputElement) => { + if (node && isWithinDialog(node)) setPortal(false); + }, + [], + ); + + const internalRef = useCallback( + (node?: HTMLInputElement) => { + forwardedRef.current = node; + checkDialog(node); + }, + [forwardedRef, checkDialog], + );frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsx (1)
144-144
: Replace empty object type with explicit type.The empty object type
{}
is flagged by static analysis as it can lead to type safety issues. Consider using a more explicit type.Apply this change to all three wrapper implementations:
-const wrapper = ({ children }: PropsWithChildren<{}>) => +const wrapper = ({ children }: PropsWithChildren<Record<string, never>>) =>Or create a reusable type:
type WrapperProps = PropsWithChildren<Record<string, never>>;Also applies to: 155-155, 166-166
🧰 Tools
🪛 Biome (1.9.4)
[error] 144-144: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.stories.tsx
(2 hunks)frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsx
(2 hunks)frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsx
(1 hunks)frontend/libs/studio-components/src/components/StudioCombobox/isWithinDialog.test.tsx
(1 hunks)frontend/libs/studio-components/src/components/StudioCombobox/isWithinDialog.ts
(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsx
[error] 144-144: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
[error] 155-155: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
[error] 166-166: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
🔇 Additional comments (7)
frontend/libs/studio-components/src/components/StudioCombobox/isWithinDialog.ts (1)
1-3
: LGTM! Clean and focused implementation.The function is well-implemented, using standard DOM APIs and proper null safety checks.
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsx (2)
12-14
: LGTM! Clean props and state management.Good implementation of:
- Default portal behavior
- Proper state initialization with prop value
- Clear prop naming convention
24-28
: LGTM! Clean component rendering.The component correctly passes the portal state to the Combobox component.
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.stories.tsx (1)
40-54
: LGTM! Well-structured story demonstrating modal context.The story effectively demonstrates the combobox behavior within a modal context, following Storybook best practices.
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsx (3)
1-7
: LGTM! Type imports enhance type safety.The new type imports and utility imports are well-organized and necessary for the enhanced test coverage.
142-177
: LGTM! Comprehensive test coverage for portal behavior and ref forwarding.The new test cases effectively validate:
- Default portal behavior
- Non-portal behavior with wrapper
- Dialog-specific behavior
- Ref forwarding functionality
These tests align perfectly with the PR's objective of fixing combobox accessibility within dialogs.
🧰 Tools
🪛 Biome (1.9.4)
[error] 144-144: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
[error] 155-155: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
[error] 166-166: Don't use '{}' as a type.
Prefer explicitly define the object shape. '{}' means "any non-nullable value".
(lint/complexity/noBannedTypes)
180-193
: LGTM! Enhanced test utility with backward compatibility.The
renderTestCombobox
function has been thoughtfully updated to support new test scenarios while maintaining compatibility with existing tests.
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsx (1)
16-26
: Consider memoizing the ref callback dependencies.The implementation correctly detects dialog context and updates portal state. However, the dependencies of the callbacks could be memoized to prevent unnecessary re-renders.
export const StudioCombobox = forwardRef<HTMLInputElement, StudioComboboxProps>( ({ children, size = 'sm', portal: givenPortal = true, ...rest }, ref): JSX.Element => { const forwardedRef = useForwardedRef<HTMLInputElement>(ref); const [portal, setPortal] = useState<boolean>(givenPortal); + const setPortalFalse = useCallback(() => setPortal(false), []); const removePortalIfInDialog = useCallback((node: HTMLInputElement | null): void => { - if (node && isWithinDialog(node)) setPortal(false); + if (node && isWithinDialog(node)) setPortalFalse(); - }, []); + }, [setPortalFalse]);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsx
(2 hunks)frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsx
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: Build environment and run e2e test
- GitHub Check: Typechecking and linting
- GitHub Check: Testing
- GitHub Check: CodeQL
🔇 Additional comments (7)
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.tsx (3)
1-1
: LGTM! Good choice of hooks and utilities.The added imports enhance the component's functionality:
useForwardedRef
for proper ref forwardingisWithinDialog
for dialog detectionAlso applies to: 6-7
12-14
: LGTM! Well-structured prop handling and ref management.Good practices observed:
- Default portal behavior preserved with
givenPortal = true
- Proper ref forwarding using the
useForwardedRef
hook
29-31
: LGTM! Clean component rendering.The component correctly applies the portal state and forwards all other props.
frontend/libs/studio-components/src/components/StudioCombobox/StudioCombobox.test.tsx (4)
1-1
: LGTM! Good test setup with proper types.The added imports enhance test type safety and provide necessary testing utilities.
Also applies to: 3-3, 7-7
142-170
: LGTM! Comprehensive portal behavior testing.The tests effectively cover:
- Default portal behavior
- Explicit portal disable
- Automatic portal disable in dialog context
172-184
: LGTM! Thorough ref forwarding tests.The tests properly verify:
- Ref forwarding to the input element
- Ref cleanup on unmount
187-200
: LGTM! Well-structured test helper.The renderTestCombobox helper is properly enhanced to support:
- Custom render options
- Ref forwarding
- Consistent test data
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.
Brilliant!
Description
The
Combobox
component from The Design System renders its list box in a portal (on the top level of the DOM) by default, which makes it inaccessible when used in a<dialog>
element. Therefore I have added this hack to always setportal
tofalse
when it is rendered within a dialog. As the Storybook demo (see image below) shows, this is not an optimal solution, but at least the list box is somewhat available to the users.We need this because the code list library need to render comboboxes within a dialog. We should switch them out with something like
Suggestion
when we have upgraded The Design System.Current solution:
![image](https://private-user-images.githubusercontent.com/29770305/412929009-327923a9-08df-4ae3-ab1b-408a833ba31e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1NzYwODMsIm5iZiI6MTczOTU3NTc4MywicGF0aCI6Ii8yOTc3MDMwNS80MTI5MjkwMDktMzI3OTIzYTktMDhkZi00YWUzLWFiMWItNDA4YTgzM2JhMzFlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDIzMjk0M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWVkNmZhYzYxNmNiMTA2NTYwNDdhMzczYTdlOTM0YWQyYTQxNmI3ZmU3YjM0ZjQxZTBmYTYyMWIzNDZkNTNjZGImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.E2VeTpXXMbN3Y9cLp4qB1bMTHPDaUhbWvmH2oclLnVI)
This pull request:
![image](https://private-user-images.githubusercontent.com/29770305/412928626-d828213b-4bf5-42f9-89df-27d0bd80a830.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1NzYwODMsIm5iZiI6MTczOTU3NTc4MywicGF0aCI6Ii8yOTc3MDMwNS80MTI5Mjg2MjYtZDgyODIxM2ItNGJmNS00MmY5LTg5ZGYtMjdkMGJkODBhODMwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTQlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE0VDIzMjk0M1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWRiMTBlZTA0ZDIzMWY4NmYwOGViYTM2M2I1YWJiMWRjNzFiZGUwNGI0Y2Y1ZWI0YmExYTI3ZjY4ODk4ZTIxMTQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.pIDemKA5VJQa0fCvgFMUXWT_JiiX7CKIEuRBeZl9tzY)
Related Issue(s)
Verification
Summary by CodeRabbit
New Features
Tests
isWithinDialog
function, ensuring accurate context detection.