Skip to content

Conversation

@kotAPI
Copy link
Collaborator

@kotAPI kotAPI commented Sep 4, 2025

Summary

  • refactor Badge to forward refs and retain data-driven styling

Testing

  • npm test
  • npm run lint

Summary by CodeRabbit

  • New Features

    • Badge now supports refs for direct DOM access.
    • Badge accepts all standard div attributes alongside existing options (e.g., className, children).
  • Refactor

    • Simplified Badge props by extending native HTML attributes for better compatibility and consistency.
    • Added a component display name to improve visibility in DevTools.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 4, 2025

Walkthrough

Refactors Badge to extend native div props, adds React.forwardRef with ref forwarded to the root div, and sets component displayName; core rendering and data attributes unchanged. (Adds tests verifying ref forwarding and data attributes.)

Changes

Cohort / File(s) Summary
Badge component (props + ref + displayName)
src/components/ui/Badge/Badge.tsx
Replaced custom BadgeProps with React.ComponentPropsWithoutRef<'div'> & { customRootClass?, variant?, size?, color? }; converted to React.forwardRef<HTMLDivElement, BadgeProps> and forwarded ref to the root div; added displayName.
Badge tests (new tests)
src/components/ui/Badge/tests/Badge.test.tsx
Added tests to assert ref forwarding to the underlying div and that data-badge-variant / data-badge-size attributes are applied.

Sequence Diagram(s)

sequenceDiagram
  participant Caller as ParentComponent
  participant Badge as Badge (forwardRef)
  participant Div as <div> (root)

  Caller->>Badge: render <Badge ref={ref} ... />
  Note right of Badge: forwardRef wrapper receives ref\nand props (children, variant, size, ...)
  Badge->>Div: render root `<div ref={ref} ... data-badge-* />`
  Div-->>Caller: DOM node available via ref.current
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

Suggested labels

automerge

Suggested reviewers

  • GoldGroove06

Poem

I’m a rabbit with a ref in paw,
Forwarding gently to the DOM I saw.
Props extended, displayName bright,
I hop through divs with tidy might.
Badge adorned — ref snug and light. 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch kotapi/fix-5-components-for-forwardrefs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

@kotAPI kotAPI requested a review from Copilot September 4, 2025 11:45
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: 0

🧹 Nitpick comments (3)
src/components/ui/Badge/Badge.tsx (3)

9-14: Props refactor looks good; consider narrowing variants and sizes.

Extending native div props is the right call. Optionally, tighten variant, size, and color with union types derived from your design tokens to improve DX and prevent invalid values.


16-16: Forwarding the ref correctly; consider exporting a helper ref type.

The forwardRef<HTMLDivElement, BadgeProps> setup is solid. For consumer ergonomics, you could also export export type BadgeRef = React.ElementRef<'div'>;.

+export type BadgeRef = React.ElementRef<'div'>;

19-24: Confirm attribute override intent; simplify composition to avoid extra closure.

Right now {...composedAttributes()} {...props} lets consumer data-* props override your standardized data attributes. If you want the component’s variant/size/color to be canonical, spread props first and place composed attributes last. Also, you can drop useComposeAttributes here and build the object once to avoid an extra function call.

-const composedAttributes = useComposeAttributes(dataAttributes(), accentAttributes());
+const composedAttributes = { ...dataAttributes(), ...accentAttributes() };

-return <div ref={ref} className={clsx(rootClass, className)} {...composedAttributes()} {...props}>
+return <div ref={ref} className={clsx(rootClass, className)} {...props} {...composedAttributes}>
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e0ca4e1 and bc1abce.

📒 Files selected for processing (1)
  • src/components/ui/Badge/Badge.tsx (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/components/ui/Badge/Badge.tsx (1)
src/core/hooks/createDataAttribute/index.ts (3)
  • useCreateDataAttribute (10-25)
  • useCreateDataAccentColorAttribute (48-55)
  • useComposeAttributes (33-40)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (1)
src/components/ui/Badge/Badge.tsx (1)

26-28: Good: forwardRef wrapper and displayName set for DevTools.

No issues here.

@kotAPI kotAPI linked an issue Sep 4, 2025 that may be closed by this pull request
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the Badge component to support ref forwarding using React.forwardRef, enabling parent components to directly access the Badge's DOM element. The refactor also updates the TypeScript props interface to extend React's built-in component props.

  • Converts Badge to use React.forwardRef for ref forwarding capability
  • Updates BadgeProps to extend React.ComponentPropsWithoutRef<'div'> for better type safety
  • Adds displayName property for improved debugging experience

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@kotAPI kotAPI merged commit cf7f403 into main Sep 4, 2025
7 checks passed
@kotAPI kotAPI deleted the kotapi/fix-5-components-for-forwardrefs branch September 4, 2025 11:55
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: 0

🧹 Nitpick comments (2)
src/components/ui/Badge/tests/Badge.test.tsx (2)

35-39: Strengthen ref test by asserting identity and element type

Non-null is weak; also assert the ref points to the rendered root and is an HTMLDivElement.

-    test('forwards refs to the underlying element', () => {
-        const ref = React.createRef<HTMLDivElement>();
-        render(<Badge ref={ref}>Badge</Badge>);
-        expect(ref.current).not.toBeNull();
-    });
+    test('forwards refs to the underlying element', () => {
+        const ref = React.createRef<HTMLDivElement>();
+        render(<Badge ref={ref}>Badge</Badge>);
+        const el = screen.getByText('Badge');
+        expect(ref.current).toBe(el);
+        expect(ref.current).toBeInstanceOf(HTMLDivElement);
+    });

41-46: Data- naming is correct; add a test ID for stability*
The data-badge-variant and data-badge-size attributes are produced by useCreateDataAttribute('badge', …), matching the intended “badge” prefix, while data-rad-ui-accent-color comes from the accent‐color hook. No changes needed to the attribute names. To stabilize the test, add a data-testid (e.g. badge) on the <Badge> and switch from getByText('Badge') to getByTestId('badge').

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between bc1abce and 60700bf.

📒 Files selected for processing (1)
  • src/components/ui/Badge/tests/Badge.test.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Badge [forwardRef refactor]

2 participants