Skip to content

variant and size for code#931

Merged
kodiakhq[bot] merged 3 commits intorad-ui:mainfrom
jindaljyoti:code_variant_size
Mar 19, 2025
Merged

variant and size for code#931
kodiakhq[bot] merged 3 commits intorad-ui:mainfrom
jindaljyoti:code_variant_size

Conversation

@jindaljyoti
Copy link
Copy Markdown
Contributor

@jindaljyoti jindaljyoti commented Mar 18, 2025

code component

image

image

Summary by CodeRabbit

  • New Features

    • Introduced interactive code examples and enhanced customization options for the code element, including new properties to control style and dimensions.
    • Added visual showcases through updated Storybook examples demonstrating various configurations.
  • Documentation

    • Expanded the documentation with dedicated sections presenting examples for different code variants, sizes, and color treatments.
  • Style

    • Enhanced styling options for the code element by integrating new visual variants and size adjustments for a more flexible and consistent UI.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 18, 2025

Walkthrough

This update introduces several new files that asynchronously retrieve source code from example files using the getSourceCodeFromPath utility. New code usage modules export objects containing the retrieved source code. Three new React components—CodeColor, CodeSizes, and CodeVariants—demonstrate different styling variants, sizes, and color applications. The documentation page now imports and renders these examples, while the Code component and its Storybook stories have been updated to support optional variant and size properties, along with corresponding style definitions in the SCSS file.

Changes

File(s) Change Summary
docs/app/docs/components/code/docs/…Usage.js (colorCodeUsage.js, sizeCodeUsage.js, variantCodeUsage.js) Added new usage files that asynchronously retrieve source code with getSourceCodeFromPath and export a structured code object.
docs/app/docs/components/code/examples/…tsx (CodeColor.tsx, CodeSizes.tsx, CodeVariants.tsx) Introduced new React components that demonstrate code examples with variations in color, size, and variant styling.
docs/app/docs/components/code/page.mdx Updated documentation to import the new code usage objects and render additional ComponentHero sections for the new examples.
src/components/ui/Code/Code.tsx Enhanced the Code component by adding optional variant and size props, conditionally setting corresponding data attributes.
src/components/ui/Code/stories/Code.stories.tsx Added new Storybook stories (Size and Variant) that showcase visual examples of the Code component with different sizes and variants.
styles/themes/components/code.scss Added new styling rules for two code variants (outline and soft) and size variations (small, medium, large, x-large).

Sequence Diagram(s)

sequenceDiagram
    participant U as Usage File
    participant G as getSourceCodeFromPath
    participant E as Example File
    participant D as Documentation Page

    U->>G: Call getSourceCodeFromPath(filePath)
    G->>E: Retrieve source code from file
    E-->>G: Return source code
    G-->>U: Return code snippet
    U->>D: Export and provide code object for rendering
Loading
sequenceDiagram
    participant S as Storybook Story
    participant C as Code Component

    S->>C: Render Code with variant & size props
    C-->>S: Return styled code snippet display
Loading

Possibly related PRs

Suggested labels

automerge

Suggested reviewers

  • GoldGroove06

Poem

I'm a rabbit hopping through lines of code,
Bringing new variants on my merry road.
Sizes and styles in a playful mix,
Async snippets delivered with clever tricks.
Hop along as our code garden grows! 🐇✨

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

docs/app/docs/components/code/docs/colorCodeUsage.js

Oops! Something went wrong! :(

ESLint: 8.56.0

ESLint couldn't find the config "next/core-web-vitals" to extend from. Please check that the name of the config is correct.

The config "next/core-web-vitals" was referenced from the config file in "/docs/.eslintrc.json".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

docs/app/docs/components/code/docs/sizeCodeUsage.js

Oops! Something went wrong! :(

ESLint: 8.56.0

ESLint couldn't find the config "next/core-web-vitals" to extend from. Please check that the name of the config is correct.

The config "next/core-web-vitals" was referenced from the config file in "/docs/.eslintrc.json".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

docs/app/docs/components/code/examples/CodeSizes.tsx

Oops! Something went wrong! :(

ESLint: 8.56.0

ESLint couldn't find the config "next/core-web-vitals" to extend from. Please check that the name of the config is correct.

The config "next/core-web-vitals" was referenced from the config file in "/docs/.eslintrc.json".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

  • 3 others

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • 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.
  • @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
Copy Markdown
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 (9)
src/components/ui/Code/Code.tsx (1)

16-16: Fix spacing around assignment operators.

The spacing around the = operators for variant and size is inconsistent with the other props.

-const Code = ({ children, customRootClass = '', color = '', variant= '', size= '' }: CodeProps) => {
+const Code = ({ children, customRootClass = '', color = '', variant = '', size = '' }: CodeProps) => {
🧰 Tools
🪛 ESLint

[error] 16-16: Operator '=' must be spaced.

(space-infix-ops)


[error] 16-16: Operator '=' must be spaced.

(space-infix-ops)

docs/app/docs/components/code/examples/CodeSizes.tsx (1)

12-16: Clean up whitespace and fix indentation

There's unnecessary whitespace between the opening and closing tags, and inconsistent indentation. This makes the code harder to read and maintain.

 <Code key={index} size={size} variant={variant}>
-                         
-                         <Text>console.log('This is some code')</Text>
-                         
-                       </Code>)
+    <Text>console.log('This is some code')</Text>
+ </Code>)
styles/themes/components/code.scss (3)

18-27: Simplify border declaration

You're declaring the same border on all four sides individually. This can be simplified to a single declaration.

&[data-code-variant="soft"]{
-        border-top: 1px solid var(--rad-ui-color-accent-600);
-        border-left: 1px solid var(--rad-ui-color-accent-600);
-        border-right: 1px solid var(--rad-ui-color-accent-600);
-        border-bottom: 1px solid var(--rad-ui-color-accent-600);
+        border: 1px solid var(--rad-ui-color-accent-600);
        color: var(--rad-ui-color-accent-950);
        background-color:  var(--rad-ui-color-accent-400);
}

24-24: Check for redundant style declaration

The background-color for the "soft" variant is identical to the default background-color defined in line 3. If this is intentional, consider adding a comment explaining why; otherwise, it can be removed to avoid redundancy.


31-53: Ensure height accommodates multiline content

Using fixed heights can cause content to overflow if it wraps to multiple lines. Consider using min-height instead of height, or adding sufficient line-height and padding to accommodate multiline content.

&[data-code-size="small"]{
    font-size: small;
    padding: 2px 8px;
-    height: 24px;
+    min-height: 24px;
}
docs/app/docs/components/code/examples/CodeColor.tsx (1)

12-16: Clean up whitespace and fix indentation

There's unnecessary whitespace and inconsistent indentation that makes the code harder to read.

 <Code key={index} size={size} variant={variant} color='pink'>
-
-                      <Text>console.log('This is some code')</Text>
-                      
-                     </Code>)
+    <Text>console.log('This is some code')</Text>
+ </Code>)
docs/app/docs/components/code/examples/CodeVariants.tsx (3)

8-11: Improve variant descriptions for clarity

The descriptions for "soft" and "outline" variants are very similar and not very descriptive. Consider making them more distinct and informative:

const codeStyleDescription = {
-  soft: 'Soft code have a soft background color and a border.',
-  outline: 'Outline code have a border and a background color.',
+  soft: 'Soft variant displays code with a colored background and matching border for a subtle, cohesive appearance.',
+  outline: 'Outline variant displays code with a transparent background and contrasting border for emphasis.'
};

22-22: Avoid inline styles

Using inline styles is generally not recommended in React applications as it makes styles harder to maintain. Consider moving this style to your CSS/SCSS file.

- <Separator orientation="horizontal" style={{ marginTop: 20 }} />
+ <Separator orientation="horizontal" className="mt-5" />

Then define the margin in your CSS/SCSS file:

.mt-5 {
  margin-top: 20px;
}

19-20: Clean up unnecessary whitespace

There's extra whitespace between the opening Code tag and the Text component. This makes the code harder to read.

<Code variant={variant} className="space-x-2">
-
-                <Text>console.log('This is some code')</Text>
+    <Text>console.log('This is some code')</Text>
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d126324 and 93666d3.

📒 Files selected for processing (10)
  • docs/app/docs/components/code/docs/colorCodeUsage.js (1 hunks)
  • docs/app/docs/components/code/docs/sizeCodeUsage.js (1 hunks)
  • docs/app/docs/components/code/docs/variantCodeUsage.js (1 hunks)
  • docs/app/docs/components/code/examples/CodeColor.tsx (1 hunks)
  • docs/app/docs/components/code/examples/CodeSizes.tsx (1 hunks)
  • docs/app/docs/components/code/examples/CodeVariants.tsx (1 hunks)
  • docs/app/docs/components/code/page.mdx (2 hunks)
  • src/components/ui/Code/Code.tsx (1 hunks)
  • src/components/ui/Code/stories/Code.stories.tsx (2 hunks)
  • styles/themes/components/code.scss (1 hunks)
🧰 Additional context used
🪛 ESLint
src/components/ui/Code/Code.tsx

[error] 16-16: Operator '=' must be spaced.

(space-infix-ops)


[error] 16-16: Operator '=' must be spaced.

(space-infix-ops)

🔇 Additional comments (12)
docs/app/docs/components/code/docs/colorCodeUsage.js (1)

1-9: Code looks well-structured and follows a consistent pattern.

The implementation correctly imports the source code parsing utility and exports the code in a structured format that can be easily consumed by documentation components.

docs/app/docs/components/code/docs/sizeCodeUsage.js (1)

1-9: Implementation follows the established pattern.

The code correctly implements the same structure as the other usage files, maintaining consistency in how example code is retrieved and exported.

docs/app/docs/components/code/docs/variantCodeUsage.js (1)

1-9: Implementation is consistent with other usage files.

The code properly implements the same pattern as the color and size usage files, ensuring consistency across the codebase.

src/components/ui/Code/Code.tsx (2)

11-12: LGTM! New props extend component configurability.

The addition of variant and size props enhances the flexibility of the Code component.


21-27: LGTM! Clean implementation of data attributes.

The conditional logic correctly adds the appropriate data attributes based on the presence of the variant and size props, following the same pattern used for the color attribute.

docs/app/docs/components/code/page.mdx (5)

4-9: LGTM: Good structured imports for new documentation sections

These imports provide access to the code examples and components needed to showcase the new variant, size, and color features of the Code component. The imports follow a consistent pattern and are organized logically.


21-23: LGTM: Well-structured Variants documentation section

This section adds documentation for Code variants by following the same pattern as the main component documentation, with appropriate title and component references.


26-28: LGTM: Well-structured Sizes documentation section

This section adds documentation for Code sizes by following the same pattern as the other documentation sections, maintaining consistency throughout the document.


30-32: LGTM: Well-structured Color documentation section

This section adds documentation for Code colors by following the same pattern as the other documentation sections, completing the feature set documentation.


34-40: LGTM: Proper component closure

The Documentation component is properly closed, maintaining the overall structure of the page with the table at the end.

src/components/ui/Code/stories/Code.stories.tsx (2)

6-7: LGTM: Good constant definitions for variants and sizes

These constants clearly define the available variants and sizes for the Code component, making them easily reusable throughout the stories.


69-84: LGTM: Clean Variant story implementation

The Variant story is well implemented, showcasing each variant option in a clear and consistent way. The structure matches other component stories in the project.

Comment on lines +8 to +19
{variants.map((variant, index) => {
return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={index} size={size} variant={variant}>

<Text>console.log('This is some code')</Text>

</Code>)
})}
</div>
})}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use unique keys instead of array indices

Using array indices as keys in React map functions can lead to rendering issues if the array order changes. Consider using the combination of variant and size for a more stable key.

- {variants.map((variant, index) => {
-     return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
+ {variants.map((variant) => {
+     return <div key={variant} className='flex justify-center w-80 flex-wrap gap-4'>
         {sizes.map((size, index) => {
             return (
-               <Code key={index} size={size} variant={variant}>
+               <Code key={`${variant}-${size}`} size={size} variant={variant}>
📝 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
{variants.map((variant, index) => {
return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={index} size={size} variant={variant}>
<Text>console.log('This is some code')</Text>
</Code>)
})}
</div>
})}
{variants.map((variant) => {
return <div key={variant} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={`${variant}-${size}`} size={size} variant={variant}>
<Text>console.log('This is some code')</Text>
</Code>)
})}
</div>
})}

Comment on lines +1 to +23
import Code from '@radui/ui/Code';
import Text from "@radui/ui/Text";

const CodeColor = () => {
const sizes = ['small', 'medium', 'large', 'x-large'];
const variants = ['soft', 'outline'];
return <div className='flex'>
{variants.map((variant, index) => {
return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={index} size={size} variant={variant} color='pink'>

<Text>console.log('This is some code')</Text>

</Code>)
})}
</div>
})}
</div>
}

export default CodeColor; No newline at end of file
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider consolidating duplicate components

This component is nearly identical to CodeSizes.tsx with the only difference being the addition of the color='pink' prop. Consider creating a more generic component that accepts props for color, or refactor to remove duplication.

// Example of a more generic component that could replace both:
const CodeExamples = ({ color }) => {
    const sizes = ['small', 'medium', 'large', 'x-large'];
    const variants = ['soft', 'outline'];
    return <div className='flex'>
        {variants.map((variant) => {
            return <div key={variant} className='flex justify-center w-80 flex-wrap gap-4'>
                {sizes.map((size) => {
                    return (
                        <Code 
                            key={`${variant}-${size}`} 
                            size={size} 
                            variant={variant} 
                            color={color}
                        >
                            <Text>console.log('This is some code')</Text>
                        </Code>
                    )
                })}
            </div>
        })}
    </div>
}

// Usage:
// <CodeExamples /> // No color, like CodeSizes
// <CodeExamples color="pink" /> // With color, like CodeColor

Comment on lines +8 to +19
{variants.map((variant, index) => {
return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={index} size={size} variant={variant} color='pink'>

<Text>console.log('This is some code')</Text>

</Code>)
})}
</div>
})}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use unique keys instead of array indices

Similar to CodeSizes.tsx, using array indices as keys can cause issues. Use the variant and size combination for more stable keys.

- {variants.map((variant, index) => {
-     return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
+ {variants.map((variant) => {
+     return <div key={variant} className='flex justify-center w-80 flex-wrap gap-4'>
         {sizes.map((size, index) => {
             return (
-                <Code key={index} size={size} variant={variant} color='pink'>
+                <Code key={`${variant}-${size}`} size={size} variant={variant} color='pink'>
📝 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
{variants.map((variant, index) => {
return <div key={index} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={index} size={size} variant={variant} color='pink'>
<Text>console.log('This is some code')</Text>
</Code>)
})}
</div>
})}
{variants.map((variant) => {
return <div key={variant} className='flex justify-center w-80 flex-wrap gap-4'>
{sizes.map((size, index) => {
return (
<Code key={`${variant}-${size}`} size={size} variant={variant} color='pink'>
<Text>console.log('This is some code')</Text>
</Code>)
})}
</div>
})}

Comment on lines +44 to +67
export const Size = () => {
return <SandboxEditor>
<div className='mt-4 mb-2'>
<p className='text-gray-950'>Code Size</p>
</div>
<div>

{Variants.map((variant, index) => (
<div key={index} className='mb-10'>
<span key={index} className="inline-flex items-start space-x-2">
{Sizes.map((size, index) => {
return <Code key={index} size={size} variant={variant}>

{Code_TEXT}

</Code>;
})}
</span>
</div>
))}

</div>
</SandboxEditor>;
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix duplicate key props in nested mapping

The Size story looks good overall, but there's a duplicate key issue in your nested mapping that could cause React warnings.

{Variants.map((variant, index) => (
-   <div key={index} className='mb-10'>
-       <span key={index} className="inline-flex items-start space-x-2">
+   <div key={`variant-${index}`} className='mb-10'>
+       <span className="inline-flex items-start space-x-2">

Using the same index as keys for both the outer and inner elements in a nested structure can lead to React key uniqueness warnings. The fix uses a prefixed key for the outer element.

📝 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
export const Size = () => {
return <SandboxEditor>
<div className='mt-4 mb-2'>
<p className='text-gray-950'>Code Size</p>
</div>
<div>
{Variants.map((variant, index) => (
<div key={index} className='mb-10'>
<span key={index} className="inline-flex items-start space-x-2">
{Sizes.map((size, index) => {
return <Code key={index} size={size} variant={variant}>
{Code_TEXT}
</Code>;
})}
</span>
</div>
))}
</div>
</SandboxEditor>;
};
export const Size = () => {
return <SandboxEditor>
<div className='mt-4 mb-2'>
<p className='text-gray-950'>Code Size</p>
</div>
<div>
{Variants.map((variant, index) => (
<div key={`variant-${index}`} className='mb-10'>
<span className="inline-flex items-start space-x-2">
{Sizes.map((size, index) => {
return <Code key={index} size={size} variant={variant}>
{Code_TEXT}
</Code>;
})}
</span>
</div>
))}
</div>
</SandboxEditor>;
};

@kotAPI kotAPI added the automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met label Mar 19, 2025
@kodiakhq kodiakhq Bot merged commit 4f09610 into rad-ui:main Mar 19, 2025
5 checks passed
This was referenced Mar 20, 2025
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