Skip to content
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

[WEB-4036] Add Badge component, add focus-base utility class #572

Merged
merged 3 commits into from
Dec 6, 2024

Conversation

jamiehenson
Copy link
Member

@jamiehenson jamiehenson commented Dec 5, 2024

Jira Ticket Link / Motivation

@aralovelace has used a temporary local badge for the latest Pricing calculator design. This PR adds a Badge component to ably-ui to support this, and other badge-oriented requirements in future.

Summary of changes

  • Adds a Badge component
  • Adds a focus-base class which replaces all of the repeated focus/focus-visible classes around the code

How do you manually test this?

📖 https://ably.github.io/ably-ui/?path=/docs/components-badge--docs

Reviewer Tasks (optional)

Merge/Deploy Checklist

  • Written automated tests for implemented features/fixed bugs
  • Rebased and squashed commits
  • Commits have clear descriptions of their changes
  • Checked for any performance regressions

Frontend Checklist

  • No frontend changes in this PR
  • Added before/after screenshots for changes
  • Tested on different platforms/browsers with Browserstack
  • Compared with the initial design / our brand guidelines
  • Checked the code for accessibility issues (VoiceOver User Guide)?

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new Badge component for displaying customizable badges.
    • Added Storybook configurations for the Badge component showcasing various states and styles.
  • Improvements

    • Updated focus styling across multiple components (Accordion, Toggle, Buttons, Dropdowns, Forms) to simplify and standardize focus visuals using the new .focus-base class.
  • Styles

    • Added new utility styles to enhance focus visibility.
    • Adjusted tracking properties for text classes to improve overall styling consistency.

These changes enhance user experience and accessibility across the application.

Copy link
Contributor

coderabbitai bot commented Dec 5, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request introduces modifications to several components and styles in the codebase. Key changes include the introduction of a new Badge component, updates to focus styling across multiple components (Accordion, Toggle, buttons, dropdowns, forms, and text), and the addition of a utility class for standardized focus management. The Badge component includes customizable properties and is accompanied by a Storybook configuration to showcase its variations. The overall structure and functionality of existing components remain intact, with no new methods or properties added.

Changes

File Change Summary
src/core/Accordion.tsx Modified AccordionTrigger className for non-static themes to simplify focus styling. No changes to props or core functionality.
src/core/Badge.tsx Introduced new Badge component with customizable properties and defined BadgeProps interface.
src/core/Badge/Badge.stories.tsx Added Storybook configuration for Badge component with multiple story variations showcasing different states and properties.
src/core/Toggle.tsx Updated Switch.Root className to simplify focus styling. No other changes to functionality.
src/core/styles.components.css Added import for utils.css and defined new component styles, including various utility classes.
src/core/styles/buttons.css Modified .ui-button-base class to replace specific focus styles with focus-base.
src/core/styles/dropdowns.css Updated .ui-dropdown-base class to simplify focus styles.
src/core/styles/forms.css Changed focus styles for .ui-input and .ui-radio classes to use focus-base.
src/core/styles/legacy-buttons.css Simplified focus styling for button classes by replacing detailed focus styles with focus-base.
src/core/styles/text.css Adjusted focus styles for .ui-link classes and modified tracking properties for text classes.
src/core/styles/utils.css Introduced new .focus-base class to standardize focus visibility styles.

Assessment against linked issues

Objective Addressed Explanation
Pricing page usage estimator (WEB-4036) No relevant changes to the pricing page.

Possibly related PRs

Suggested reviewers

  • aralovelace
  • kennethkalmer

🐇 In the code we hop and play,
With badges bright and styles that sway.
Focus simplified, a joyful cheer,
For every click, we bring you near!
So let us leap with joy today,
In our UI world, we find our way! 🐇


🪧 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 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: 3

🧹 Outside diff range and nitpick comments (5)
src/core/styles/utils.css (1)

1-3: Well-structured focus utility class that promotes accessibility!

The .focus-base utility class effectively consolidates focus styles and follows accessibility best practices by:

  • Using focus-visible for better user experience
  • Providing clear visual indicators with 4px outline
  • Maintaining consistent focus styling across components

Consider documenting this utility class in your component library's style guide to promote reuse and maintain consistency across teams.

src/core/Badge/Badge.stories.tsx (1)

12-53: Consider adding more story variations for comprehensive coverage.

The current stories cover basic functionality well, but consider adding these variations:

  • Color variants
  • Size variations
  • Combined states (e.g., hoverable + focusable)
  • Error states
  • Loading states

Here's a suggested addition:

export const WithCombinedStates = Template.bind({});
WithCombinedStates.args = {
  children: "BADGE",
  hoverable: true,
  focusable: true,
  iconBefore: "icon-gui-check-circled"
};

export const WithError = Template.bind({});
WithError.args = {
  children: "ERROR",
  color: "error",
  iconBefore: "icon-gui-warning"
};
src/core/styles/buttons.css (2)

3-3: LGTM! Consider deprecating legacy button styles

The modern button implementation with focus-base is clean and well-integrated. Consider gradually migrating components from legacy-buttons.css to this newer implementation to maintain consistency.

Migration strategy:

  1. Document the new button system as the preferred approach
  2. Mark legacy button classes as deprecated
  3. Create a migration guide for teams
  4. Set up deprecation warnings for legacy class usage

3-3: Consider documenting focus-base implementation guidelines

The introduction of focus-base is a good step toward standardization, but its implementation varies across files. To ensure consistent adoption:

  1. Document the intended usage patterns of focus-base
  2. Specify whether it should be used alone or combined with other focus states
  3. Provide examples of correct implementation in different contexts (buttons, dropdowns, etc.)

Would you like help creating this documentation or setting up a style guide for focus management?

Also applies to: 3-3, 3-3

src/core/Badge.tsx (1)

69-91: Simplify the childClass computation logic

The current implementation has duplicate cases that can be simplified.

Consider this more concise implementation:

  const childClass = useMemo(() => {
-   switch (size) {
-     case "xs":
-     case "sm":
-       return "leading-[18px]";
-     case "md":
-     case "lg":
-       return "leading-[20px]";
-   }
+   return ["xs", "sm"].includes(size) ? "leading-[18px]" : "leading-[20px]";
  }, [size]);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1c19ce9 and efbd0a7.

⛔ Files ignored due to path filters (2)
  • src/core/Badge/__snapshots__/Badge.stories.tsx.snap is excluded by !**/*.snap
  • src/core/Toggle/__snapshots__/Toggle.stories.tsx.snap is excluded by !**/*.snap
📒 Files selected for processing (11)
  • src/core/Accordion.tsx (1 hunks)
  • src/core/Badge.tsx (1 hunks)
  • src/core/Badge/Badge.stories.tsx (1 hunks)
  • src/core/Toggle.tsx (1 hunks)
  • src/core/styles.components.css (1 hunks)
  • src/core/styles/buttons.css (1 hunks)
  • src/core/styles/dropdowns.css (1 hunks)
  • src/core/styles/forms.css (2 hunks)
  • src/core/styles/legacy-buttons.css (5 hunks)
  • src/core/styles/text.css (1 hunks)
  • src/core/styles/utils.css (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/core/styles.components.css
🔇 Additional comments (9)
src/core/Toggle.tsx (1)

16-17: Good adoption of focus-base utility while maintaining accessibility!

The switch component properly implements the new focus-base utility while preserving important features:

  • Proper transitions
  • Disabled states
  • ARIA attributes (via Radix UI)

Let's verify that the focus styles are working correctly across all states:

✅ Verification successful

The previous search was too broad. Let's specifically look for Toggle-related test files and their contents to verify if there are proper accessibility tests in place, particularly for focus states.


We need to verify the focus-base utility implementation and the Toggle component's code to ensure the focus styles are properly implemented. The stories show basic functionality, but we need to see the actual implementation details.


Focus styles are properly implemented and consistent with design system

The Toggle component correctly implements the focus-base utility, which provides a standardized focus state across the design system:

  • Uses Tailwind's focus-base utility that applies a 4px outline with proper offset and color
  • Maintains accessibility through Radix UI's Switch component with proper ARIA attributes
  • Focus styles are consistently applied alongside other interactive states (checked, disabled)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any test files related to Toggle component
fd -e test.tsx -e test.ts -e spec.tsx -e spec.ts Toggle

# Check if there are any existing accessibility tests
rg -l "accessibility|a11y|focus|keyboard" $(fd -e test.tsx -e test.ts -e spec.tsx -e spec.ts Toggle)

Length of output: 1779


Script:

#!/bin/bash
# Let's check the Toggle component's test files and stories
fd Toggle.test -e tsx -e ts
fd Toggle.stories -e tsx -e ts

# Get the content of any Toggle test or story files to check for accessibility testing
fd Toggle -e test.tsx -e test.ts -e stories.tsx -e stories.ts -x cat {}

# Check for any accessibility-related tests in the Toggle component
rg -A 5 -B 5 "accessibility|a11y|focus|keyboard" src/core/Toggle

Length of output: 4368


Script:

#!/bin/bash
# Let's check the focus-base utility implementation
rg -A 5 -B 5 "focus-base" src/core/styles/

# Check the Toggle component implementation
cat src/core/Toggle.tsx

Length of output: 9335

src/core/Badge/Badge.stories.tsx (1)

20-20: Verify icon naming convention consistency.

The stories use icons with names like icon-gui-check-lotus-circled, icon-gui-chevron-down, and icon-gui-check-circled. Let's verify this naming pattern is consistent with the design system.

Also applies to: 26-26, 44-44, 51-51

✅ Verification successful

Based on the search results, I can now generate the final response as we have comprehensive information about icon naming patterns in the codebase.

Icon naming convention is consistent with the design system

The icon names used in Badge.stories.tsx follow the established pattern in the codebase. The icon-gui- prefix is consistently used across the codebase for GUI-related icons, and these icons are properly typed through the IconName type system defined in src/core/Icon/types.ts. The specific icons used:

  • icon-gui-check-lotus-circled
  • icon-gui-chevron-down
  • icon-gui-check-circled

are part of the GUI icon set (iconNames.gui) and follow the same naming convention used throughout the component library.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for icon usage patterns
rg "icon-gui-" -t tsx -t ts

# Check for icon definitions or configurations
fd -e tsx -e ts -e css | xargs rg "icon-gui-|iconName|IconName"

Length of output: 8580

src/core/styles/dropdowns.css (1)

3-3: Verify accessibility of new focus styling

While consolidating focus styles with focus-base improves consistency, we should verify that it maintains proper keyboard navigation visibility and meets WCAG requirements for focus indicators.

✅ Verification successful

Let me check the definition of focus-base to verify its accessibility properties.


Focus styling meets accessibility standards

The focus-base utility provides strong accessibility features through:

  • Visible focus indicators using focus-visible:outline-4
  • High-contrast outline via focus-visible:outline-gui-focus
  • Proper keyboard navigation support with focus-visible pseudo-class
  • Consistent implementation across buttons, inputs, and interactive elements

This consolidation maintains WCAG compliance while improving design consistency.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for focus-base definition to verify it meets accessibility standards
rg -l "focus-base" "src/core/styles/"

# Check for any existing accessibility tests
fd -e test.js -e test.tsx -e spec.js -e spec.tsx | xargs rg -l "accessibility|focus|keyboard"

Length of output: 1896


Script:

#!/bin/bash
# Check the definition of focus-base in utils.css
rg "focus-base" src/core/styles/utils.css -A 5 -B 5

# Look for any accessibility-related comments or documentation
rg "@apply.*focus" src/core/styles/ -A 2 -B 2

Length of output: 6523

src/core/Badge.tsx (2)

1-55: LGTM! Well-structured props interface with comprehensive documentation.

The BadgeProps interface is well-designed with:

  • Clear JSDoc documentation for each prop
  • Appropriate type constraints
  • Good default values in the implementation

93-112: LGTM! Comprehensive color mapping with dark mode support.

The color class computation is well-implemented with:

  • Consistent color mapping
  • Dark mode consideration for neutral color
  • Appropriate performance optimization
src/core/styles/forms.css (1)

126-126: LGTM! Consistent implementation of focus-base utility

The changes successfully standardize focus styles across form elements:

  • ui-input: Replaced individual focus styles with focus-base
  • ui-radio: Unified focus handling with other components

Also applies to: 143-143

src/core/styles/text.css (1)

179-180: LGTM! Standardized focus styles for links

The changes successfully implement the focus-base utility while maintaining the existing link behaviors:

  • ui-link: Preserves default text color during focus
  • ui-link-neutral: Maintains charcoal-grey text color during focus

Also applies to: 185-186

src/core/Accordion.tsx (2)

100-102: LGTM: Focus styling maintains accessibility

The conditional application of focus-base for non-static themes while preserving focus-visible:outline-none for static themes maintains proper keyboard navigation accessibility. This change aligns well with the PR objective of standardizing focus management across components.


101-101: Verify the focus-base utility class implementation

The change to use the new focus-base utility class looks good, but let's verify its implementation to ensure it provides equivalent styling to the replaced focus classes.

✅ Verification successful

Focus-base utility class is properly implemented and widely adopted

The focus-base utility class is well-defined in utils.css with proper focus management styles using outline properties. It's already being used consistently across multiple components (Badge, Toggle, Accordion) and applies standardized focus styling through Tailwind's @apply directive. The implementation in Accordion maintains accessibility by applying focus styles only for interactive themes.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the focus-base utility class exists and check its implementation
# Expected: Should find the utility class definition with equivalent focus styles

# Search for the focus-base utility class definition
echo "Searching for focus-base utility class definition..."
rg -t css "focus-base" 

# Search for any other components using the new utility class
echo "Checking adoption of focus-base across components..."
rg "focus-base" --type tsx

Length of output: 1466


Script:

#!/bin/bash
# Let's try searching for .tsx files using ripgrep's file extension pattern
echo "Checking adoption of focus-base across components..."
rg "focus-base" -g "*.tsx"

# Let's also check the implementation of focus-base utility class
echo "Checking focus-base utility class implementation..."
rg -A 5 "\.focus-base" src/core/styles/utils.css

Length of output: 948

src/core/styles/legacy-buttons.css Show resolved Hide resolved
src/core/Badge.tsx Show resolved Hide resolved
src/core/Badge.tsx Outdated Show resolved Hide resolved
Copy link
Member

@kennethkalmer kennethkalmer left a comment

Choose a reason for hiding this comment

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

LGTM, will defer final ✅ to @aralovelace because this directly affects her work :D

src/core/Badge.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@aralovelace aralovelace left a comment

Choose a reason for hiding this comment

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

Just small feedback

src/core/Badge.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@aralovelace aralovelace left a comment

Choose a reason for hiding this comment

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

LGTM and provide the requirements for the estimator

@jamiehenson jamiehenson merged commit bee9a6f into main Dec 6, 2024
4 checks passed
@jamiehenson jamiehenson deleted the badge-component branch December 6, 2024 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

3 participants