Skip to content

Conversation

@GoldGroove06
Copy link
Contributor

@GoldGroove06 GoldGroove06 commented Dec 21, 2024

#220. Visually hidden component with support for asChild as renders as div as default in DOM.

Summary by CodeRabbit

  • New Features

    • Introduced the VisuallyHidden component for rendering content that is visually hidden but accessible to screen readers.
    • Added a new CSS class .rad-ui-visually-hidden for visually hiding elements while maintaining accessibility.
  • Documentation

    • Created Storybook stories for the VisuallyHidden component to demonstrate its usage and variations.
  • Tests

    • Added a test suite to verify the rendering behavior of the VisuallyHidden component under various conditions.
  • Chores

    • Updated the main SCSS file to include the new visually hidden styles.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 21, 2024

Walkthrough

The pull request introduces a new VisuallyHidden React component designed to render content that is visually hidden but remains accessible to screen readers. The implementation includes the component itself, Storybook stories for documentation, comprehensive test cases, and corresponding SCSS styling. The component allows for customization through props and provides flexibility in rendering, supporting both default and child element rendering modes.

Changes

File Change Summary
src/components/ui/VisuallyHidden/VisuallyHidden.tsx New React component for visually hidden content with customizable props
src/components/ui/VisuallyHidden/stories/VisuallyHidden.stories.tsx Added Storybook stories demonstrating component usage and variations
src/components/ui/VisuallyHidden/tests/VisuallyHidden.test.js Comprehensive test suite covering component rendering scenarios
styles/themes/components/visuallyhidden.scss New CSS class .rad-ui-visually-hidden for hiding elements
styles/themes/default.scss Added import for visuallyhidden component styles

Sequence Diagram

sequenceDiagram
    participant Component as VisuallyHidden
    participant Renderer as React Renderer
    participant Styles as CSS Styles
    participant Accessibility as Screen Reader

    Component->>Renderer: Render with props
    Renderer->>Styles: Apply visually hidden class
    Renderer->>Accessibility: Maintain content accessibility
    Accessibility-->>Renderer: Content readable
Loading

Poem

🐰 A Rabbit's Ode to Hidden Delight

Invisible text, yet clear as light,
Whispers softly beyond sight,
Accessibility's gentle embrace,
Hiding pixels with digital grace,
A component that speaks without a sound! 🔊

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (5)
src/components/ui/VisuallyHidden/stories/VisuallyHidden.stories.tsx (3)

1-1: Remove unused import

The Children import from React is not used in this file.

-import React, { Children } from 'react';
+import React from 'react';

9-13: Fix indentation in render function

The indentation of the children prop is inconsistent.

     render: (args: VisuallyHiddenProps) => <SandboxEditor>
         <VisuallyHidden {...args}>
-                {args.children}
-            </VisuallyHidden>
+            {args.children}
+        </VisuallyHidden>
     </SandboxEditor>

18-30: Add proper documentation for component props

The stories should document the available props and their purpose.

Add the following above the Default story:

/**
 * Props:
 * - children: Content to be visually hidden
 * - className: Additional CSS classes
 * - customRootClass: Override default root class
 * - asChild: Render children directly instead of wrapping div
 */
🧰 Tools
🪛 eslint

[error] 21-21: Unexpected trailing comma.

(comma-dangle)


[error] 28-28: Unexpected trailing comma.

(comma-dangle)

styles/themes/default.scss (2)

26-26: Consider maintaining alphabetical import order.

While the import addition is correct, consider moving it before switch.scss to maintain alphabetical ordering with other component imports.

 @import "./components/separator.scss";
 @import "./components/skeleton.scss";
+@import "./components/visuallyhidden.scss";
 @import "./components/switch.scss";
-@import "./components/visuallyhidden.scss";

Line range hint 57-61: Remove empty CSS rules.

The .rad-ui-tooltip rule is empty and can be removed. If it's intended for future use, consider adding a TODO comment or implementing it now.

-.rad-ui-tooltip{
-    
-}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 16139dc and ee41843.

📒 Files selected for processing (5)
  • src/components/ui/VisuallyHidden/VisuallyHidden.tsx (1 hunks)
  • src/components/ui/VisuallyHidden/stories/VisuallyHidden.stories.tsx (1 hunks)
  • src/components/ui/VisuallyHidden/tests/VisuallyHidden.test.js (1 hunks)
  • styles/themes/components/visuallyhidden.scss (1 hunks)
  • styles/themes/default.scss (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • styles/themes/components/visuallyhidden.scss
🧰 Additional context used
🪛 eslint
src/components/ui/VisuallyHidden/VisuallyHidden.tsx

[error] 15-17: Block must not be padded by blank lines.

(padded-blocks)


[error] 18-20: Block must not be padded by blank lines.

(padded-blocks)


[error] 23-23: Newline required at end of file but not found.

(eol-last)

src/components/ui/VisuallyHidden/tests/VisuallyHidden.test.js

[error] 5-6: More than 1 blank line not allowed.

(no-multiple-empty-lines)


[error] 19-19: Expected an assignment or function call and instead saw an expression.

(no-unused-expressions)


[error] 24-24: Expected an assignment or function call and instead saw an expression.

(no-unused-expressions)


[error] 26-26: Newline required at end of file but not found.

(eol-last)

src/components/ui/VisuallyHidden/stories/VisuallyHidden.stories.tsx

[error] 16-17: More than 1 blank line not allowed.

(no-multiple-empty-lines)


[error] 21-21: Unexpected trailing comma.

(comma-dangle)


[error] 28-28: Unexpected trailing comma.

(comma-dangle)

🪛 GitHub Check: lint
src/components/ui/VisuallyHidden/tests/VisuallyHidden.test.js

[failure] 18-18:
Expected an assignment or function call and instead saw an expression


[failure] 23-23:
Expected an assignment or function call and instead saw an expression

🔇 Additional comments (3)
src/components/ui/VisuallyHidden/tests/VisuallyHidden.test.js (1)

1-1: Verify CSS implementation for visual hiding

The component relies on the rad-ui-visually-hidden class, but the SCSS implementation isn't included in the review.

styles/themes/default.scss (2)

26-26: Verify visuallyhidden.scss implementation.

Let's ensure the imported file exists and contains appropriate visually-hidden styles that maintain accessibility.


Line range hint 31-36: Review commented-out focus styles for accessibility impact.

The commented-out focus styles might be important for accessibility. Since this PR introduces an accessibility-focused component, we should ensure that proper focus management is in place.

Let's check if focus styles are defined elsewhere:

✅ Verification successful

Focus styles are properly implemented in component-specific files

The commented-out focus styles in default.scss can safely remain commented as each component has its own focus management implemented using modern :focus-visible or :focus selectors with appropriate outlines:

  • Button: Uses :focus-visible with 2px solid accent-900
  • Toggle & Toggle Group: Uses :focus-visible with 2px solid accent-900
  • Textarea: Uses :focus with 2px solid accent-500
  • Radio: Uses :focus with 2px solid accent-900
  • Accordion: Intentionally removes outline but uses :focus-within for container focus

The component-specific implementations provide better focus management than the generic styles that were commented out, and they follow modern accessibility practices by using :focus-visible where appropriate.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for focus-related styles in other files
rg -g '*.{scss,css}' '(:focus|outline:|focus-visible)' --type css

Length of output: 1632

Comment on lines +15 to +19
const VisuallyHidden = ({ children, customRootClass, className, ...props }: VisuallyHiddenProps) => {

const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
return <Primitive.div className={clsx(rootClass, className)} {...props}>{children}</Primitive.div>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add proper CSS class for visual hiding and ARIA attributes

The component is missing:

  1. The actual CSS class that handles visual hiding
  2. Proper ARIA attributes for accessibility

Apply this fix:

 const VisuallyHidden = ({ children, customRootClass, className, ...props }: VisuallyHiddenProps) => {
     const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
-    return <Primitive.div className={clsx(rootClass, className)} {...props}>{children}</Primitive.div>
+    return (
+        <Primitive.div 
+            className={clsx(rootClass, 'rad-ui-visually-hidden', className)}
+            aria-hidden="false"
+            {...props}
+        >
+            {children}
+        </Primitive.div>
+    );
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const VisuallyHidden = ({ children, customRootClass, className, ...props }: VisuallyHiddenProps) => {
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
return <Primitive.div className={clsx(rootClass, className)} {...props}>{children}</Primitive.div>
const VisuallyHidden = ({ children, customRootClass, className, ...props }: VisuallyHiddenProps) => {
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
return (
<Primitive.div
className={clsx(rootClass, 'rad-ui-visually-hidden', className)}
aria-hidden="false"
{...props}
>
{children}
</Primitive.div>
);
}
🧰 Tools
🪛 eslint

[error] 15-17: Block must not be padded by blank lines.

(padded-blocks)

Comment on lines +6 to +26
describe('VisuallyHidden Component', () => {
test('renders VisuallyHidden component', () => {
render(<VisuallyHidden>Visually Hidden</VisuallyHidden>);
expect(screen.getByText('Visually Hidden')).toBeInTheDocument();
});

test('renders VisuallyHidden component with className', () => {
render(<VisuallyHidden className='extra-classname' >Visually Hidden</VisuallyHidden>);
expect(screen.getByText('Visually Hidden')).toHaveClass('extra-classname');
});

test('renders VisuallyHidden component without asChild prop', () => {
render(<VisuallyHidden >Visually Hidden</VisuallyHidden>);
expect(screen.getByText('Visually Hidden')).tagName === 'DIV';
});

test('renders VisuallyHidden component with asChild prop as span', () => {
render(<VisuallyHidden asChild><span>Visually Hidden</span></VisuallyHidden>);
expect(screen.getByText('Visually Hidden')).tagName === 'SPAN';
});
}) No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add missing test cases

The test suite should cover:

  1. customRootClass prop functionality
  2. Presence of rad-ui-visually-hidden class
  3. ARIA attributes

Add these test cases:

    test('renders with custom root class', () => {
        render(<VisuallyHidden customRootClass="custom-root">Content</VisuallyHidden>);
        expect(screen.getByText('Content')).toHaveClass('custom-root');
    });

    test('applies visually-hidden class', () => {
        render(<VisuallyHidden>Content</VisuallyHidden>);
        expect(screen.getByText('Content')).toHaveClass('rad-ui-visually-hidden');
    });

    test('has correct aria attributes', () => {
        render(<VisuallyHidden>Content</VisuallyHidden>);
        expect(screen.getByText('Content')).toHaveAttribute('aria-hidden', 'false');
    });
🧰 Tools
🪛 eslint

[error] 19-19: Expected an assignment or function call and instead saw an expression.

(no-unused-expressions)


[error] 24-24: Expected an assignment or function call and instead saw an expression.

(no-unused-expressions)


[error] 26-26: Newline required at end of file but not found.

(eol-last)

🪛 GitHub Check: lint

[failure] 18-18:
Expected an assignment or function call and instead saw an expression


[failure] 23-23:
Expected an assignment or function call and instead saw an expression

@kotAPI kotAPI added the automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met label Dec 21, 2024
@kodiakhq kodiakhq bot merged commit 4409f00 into rad-ui:main Dec 21, 2024
4 of 5 checks passed
@GoldGroove06 GoldGroove06 deleted the visually-hidden-component branch December 22, 2024 06:03
@coderabbitai coderabbitai bot mentioned this pull request Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants