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

feat: enhance support for the nested theme token structure #958

Merged
merged 8 commits into from
Dec 18, 2024
Merged

Conversation

cheton
Copy link
Member

@cheton cheton commented Dec 17, 2024

PR Type

Enhancement


Description

  • Enhanced the theme token system to support nested structures with a value property
  • Added ability to use dot notation to access nested theme tokens (e.g. color="white.primary")
  • The getter function now extracts the value property from object results, enabling more flexible theme token definitions
  • Improved backwards compatibility by maintaining fallback to original value when result is undefined

Changes walkthrough 📝

Relevant files
Enhancement
transforms.js
Support nested theme token structure with value property 

packages/styled-system/src/utils/transforms.js

  • Added support for nested theme token structure by extracting the value
    property from object results
  • Enhanced the getter function to handle theme tokens with nested object
    structure
  • Added comprehensive example documentation showing usage with color
    tokens
  • +37/-1   

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    codesandbox bot commented Dec 17, 2024

    Review or Edit in CodeSandbox

    Open the branch in Web EditorVS CodeInsiders

    Open Preview

    Copy link

    changeset-bot bot commented Dec 17, 2024

    🦋 Changeset detected

    Latest commit: 562b05f

    The changes in this PR will be included in the next version bump.

    This PR includes changesets to release 1 package
    Name Type
    @tonic-ui/styled-system Minor

    Not sure what this means? Click here to learn what changesets are.

    Click here if you're a maintainer who wants to add another changeset to this PR

    Copy link
    Contributor

    codiumai-pr-agent-free bot commented Dec 17, 2024

    PR Reviewer Guide 🔍

    (Review updated until commit 4c99f66)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Code Smell
    The getter function contains a FIXME comment about deprecated properties that should be addressed before merging

    Code Duplication
    There is duplicated logic for handling non-negative values and scale object checks in two separate code blocks

    Copy link
    Contributor

    codiumai-pr-agent-free bot commented Dec 17, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Handle deeply nested object structures recursively to support multi-level theme tokens

    Consider handling nested objects recursively to support deeper theme token
    structures with multiple levels of value properties.

    packages/styled-system/src/utils/transforms.js [64-66]

    -if (typeof result === 'object') {
    -  result = result?.value;
    +while (result && typeof result === 'object' && 'value' in result) {
    +  result = result.value;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion significantly improves the robustness of theme token handling by supporting deeply nested structures, which is important for complex theme configurations. The recursive approach ensures all levels of nested 'value' properties are properly resolved.

    8
    Possible issue
    Add null check to prevent runtime errors when accessing properties of null objects

    Add null check for result before accessing the value property to prevent potential
    runtime errors when accessing properties of null.

    packages/styled-system/src/utils/transforms.js [64-66]

    -if (typeof result === 'object') {
    +if (result && typeof result === 'object') {
       result = result?.value;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding a null check is a good defensive programming practice that prevents potential runtime errors when dealing with null objects. While the existing code uses optional chaining (?.), an explicit null check provides additional safety.

    7

    Copy link

    codecov bot commented Dec 17, 2024

    Bundle Report

    Changes will increase total bundle size by 752 bytes (0.03%) ⬆️. This is within the configured threshold ✅

    Detailed changes
    Bundle name Size Change
    @tonic-ui/styled-system-cjs 62.2kB 97 bytes (-0.16%) ⬇️
    @tonic-ui/styled-system-esm 59.98kB 849 bytes (1.44%) ⬆️

    Copy link

    codesandbox-ci bot commented Dec 17, 2024

    This pull request is automatically built and testable in CodeSandbox.

    To see build info of the built libraries, click here or the icon next to each commit SHA.

    @cheton cheton linked an issue Dec 17, 2024 that may be closed by this pull request
    Copy link

    codecov bot commented Dec 17, 2024

    Codecov Report

    Attention: Patch coverage is 98.63014% with 1 line in your changes missing coverage. Please review.

    Project coverage is 78.46%. Comparing base (46dcfc1) to head (562b05f).
    Report is 1 commits behind head on v2.

    Files with missing lines Patch % Lines
    packages/styled-system/src/core/parser.js 96.66% 1 Missing ⚠️
    Additional details and impacted files
    @@            Coverage Diff             @@
    ##               v2     #958      +/-   ##
    ==========================================
    + Coverage   78.44%   78.46%   +0.02%     
    ==========================================
      Files         406      403       -3     
      Lines        6693     6692       -1     
    ==========================================
    + Hits         5250     5251       +1     
    + Misses       1443     1441       -2     

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    @trendmicro-frontend-bot
    Copy link
    Contributor

    trendmicro-frontend-bot commented Dec 17, 2024

    Tonic UI Demo

    On 2024-12-18 11:41:22 +0000, PR #958 (562b05f) was successfully deployed. You can view it at the following link:
    https://trendmicro-frontend.github.io/tonic-ui-demo/react/pr-958/

    @cheton
    Copy link
    Member Author

    cheton commented Dec 18, 2024

    /ask Provide several Jest tests with the new theme token structure for the PR. Here is the example usage:

    <Box color="white.primary" />
    <Box color="black.primary" />

    The colors scale in the theme:

    {
      colors: {
        white: {
          primary: {
            value: 'rgba(255, 255, 255, .92)',
          },
          secondary: {
            value: 'rgba(255, 255, 255, .60)',
          },
        },
        black: {
          primary: {
            value: 'rgba(0, 0, 0, .92)',
          },
          secondary: {
            value: 'rgba(0, 0, 0, .65)',
          },
        },
      },
    }

    Copy link
    Contributor

    Ask

    Provide several Jest tests with the new theme token structure for the PR. Here is the example usage: jsx <Box color=white.primary /> <Box color=black.primary /> The colors scale in the theme: js { colors: { white: { primary: { value: 'rgba(255, 255, 255, .92)', }, secondary: { value: 'rgba(255, 255, 255, .60)', }, }, black: { primary: { value: 'rgba(0, 0, 0, .92)', }, secondary: { value: 'rgba(0, 0, 0, .65)', }, }, }, }

    Answer:

    Here are several Jest tests for the new theme token structure:

    describe('Theme token structure', () => {
      const theme = {
        colors: {
          white: {
            primary: {
              value: 'rgba(255, 255, 255, .92)',
            },
            secondary: {
              value: 'rgba(255, 255, 255, .60)',
            },
          },
          black: {
            primary: {
              value: 'rgba(0, 0, 0, .92)',
            },
            secondary: {
              value: 'rgba(0, 0, 0, .65)',
            },
          },
        },
      };
    
      it('should resolve nested color tokens with dot notation', () => {
        const result = getter(theme.colors, 'white.primary');
        expect(result).toBe('rgba(255, 255, 255, .92)');
      });
    
      it('should resolve secondary color variants', () => {
        const result = getter(theme.colors, 'black.secondary');
        expect(result).toBe('rgba(0, 0, 0, .65)');
      });
    
      it('should fallback to original value when token path does not exist', () => {
        const result = getter(theme.colors, 'white.nonexistent');
        expect(result).toBe('white.nonexistent');
      });
    
      it('should handle undefined theme values', () => {
        const result = getter(undefined, 'white.primary');
        expect(result).toBe('white.primary');
      });
    
      it('should handle nested objects without value property', () => {
        const customTheme = {
          colors: {
            custom: {
              primary: 'rgb(100, 100, 100)',
            },
          },
        };
        const result = getter(customTheme.colors, 'custom.primary');
        expect(result).toBe('rgb(100, 100, 100)');
      });
    });

    These tests cover:

    1. Basic nested token resolution using dot notation
    2. Different color variants (primary/secondary)
    3. Fallback behavior for non-existent paths
    4. Handling of undefined theme values
    5. Backwards compatibility with objects that don't use the value property

    Each test verifies a different aspect of the new theme token structure functionality while ensuring the getter function works as expected.

    @cheton
    Copy link
    Member Author

    cheton commented Dec 18, 2024

    /ask Enhance test coverage in "transforms.test.js" by adding test cases for the positiveOrNegative transform.

    Copy link
    Contributor

    Ask

    Enhance test coverage in transforms.test.js by adding test cases for the positiveOrNegative transform.

    Answer:

    I'll help you create test cases for the positiveOrNegative transform to enhance test coverage. Based on the code context, we should add tests that verify both positive and negative value handling. Here's a suggested test suite to add to the existing transforms.test.js file:

    describe('positiveOrNegative', () => {
      const scale = {
        1: '4px',
        2: '8px',
        3: '12px',
        4: '16px'
      };
    
      it('should handle positive values', () => {
        expect(positiveOrNegative(scale, '2')).toBe('8px');
        expect(positiveOrNegative(scale, 3)).toBe('12px');
        expect(positiveOrNegative(scale, '4')).toBe('16px');
      });
    
      it('should handle negative values', () => {
        expect(positiveOrNegative(scale, '-2')).toBe('-8px');
        expect(positiveOrNegative(scale, -3)).toBe('-12px');
        expect(positiveOrNegative(scale, '-4')).toBe('-16px');
      });
    
      it('should return original value when scale value is not found', () => {
        expect(positiveOrNegative(scale, '5')).toBe('5');
        expect(positiveOrNegative(scale, '-5')).toBe('-5');
      });
    
      it('should handle undefined scale', () => {
        expect(positiveOrNegative(undefined, '2')).toBe('2');
        expect(positiveOrNegative(undefined, '-2')).toBe('-2');
      });
    
      it('should handle non-numeric strings', () => {
        expect(positiveOrNegative(scale, 'auto')).toBe('auto');
        expect(positiveOrNegative(scale, '-auto')).toBe('-auto');
      });
    });

    These test cases cover:

    1. Positive value resolution from the scale
    2. Negative value resolution from the scale
    3. Fallback behavior when scale values are not found
    4. Handling of undefined scale
    5. Handling of non-numeric string values

    You'll need to import the positiveOrNegative function at the top of the test file:

    import { getter, positiveOrNegative } from '../transforms';

    This test suite will provide comprehensive coverage for the positiveOrNegative transform functionality and help ensure its reliability across different use cases.

    @cheton
    Copy link
    Member Author

    cheton commented Dec 18, 2024

    /review

    Copy link
    Contributor

    Persistent review updated to latest commit 4c99f66

    @cheton cheton merged commit 32c71ec into v2 Dec 18, 2024
    8 checks passed
    @cheton cheton deleted the tonic-ui-956 branch December 18, 2024 11:48
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    Enhance support for the nested theme token structure
    2 participants