Skip to content

Feat: rules() props runtime #131#150

Merged
github-actions[bot] merged 1 commit intomainfrom
runtime-props
Feb 10, 2025
Merged

Feat: rules() props runtime #131#150
github-actions[bot] merged 1 commit intomainfrom
runtime-props

Conversation

@black7375
Copy link
Contributor

@black7375 black7375 commented Feb 10, 2025

Description

Implement rules() props.

Related Issue

Summary by CodeRabbit

  • New Features

    • Enhanced CSS styling configuration to support dynamic mapping of property values to CSS variables.
    • Introduced a utility for extracting CSS variable identifiers from style declarations.
  • Tests

    • Added tests to verify the new CSS variable extraction functionality.
  • Refactor

    • Improved internal type consistency and API signatures for more robust styling and enhanced error prevention.

Additional context

Checklist

@changeset-bot
Copy link

changeset-bot bot commented Feb 10, 2025

⚠️ No Changeset found

Latest commit: 7899606

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2025

Warning

Rate limit exceeded

@black7375 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 57 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between afb191f and 7899606.

📒 Files selected for processing (5)
  • packages/css/src/rules/createRuntimeFn.ts (3 hunks)
  • packages/css/src/rules/index.ts (12 hunks)
  • packages/css/src/rules/types.ts (7 hunks)
  • packages/css/src/utils.ts (1 hunks)
  • packages/transform-to-vanilla/src/types/style-rule.ts (1 hunks)

Walkthrough

This pull request enhances the CSS rules module by refining type definitions and function signatures. The changes introduce a new generic parameter for property definitions in the runtime function, update the handling of CSS properties, and add utility support for extracting CSS variable names. Updates span multiple files including core logic, types, and tests, thereby ensuring tighter type safety and improved property processing in CSS rule generation.

Changes

File(s) Summary of Changes
packages/css/src/rules/createRuntimeFn.ts
packages/css/src/rules/index.ts
packages/css/src/rules/types.ts
Updated type definitions and function signatures to include a new generic Props parameter. Added a new runtimeFn.props method for mapping properties to CSS variables, introduced the processPropObject function, and enhanced overall type safety with additional type imports and updated return types.
packages/css/src/utils.ts Added the getVarName function to extract CSS variable names from strings and included corresponding tests using import.meta.vitest. Integrated dependencies such as createVar and setFileScope to support the functionality.
packages/transform-to-vanilla/src/types/style-rule.ts Introduced the PureCSSVarKey and PureCSSVarFunction types, and updated existing CSSVarKey and CSSVarFunction types to incorporate these new definitions, improving the specificity of CSS variable type handling.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RuntimeFn
    participant Config

    Client->>RuntimeFn: Invoke createRuntimeFn with config & props
    RuntimeFn->>Config: Retrieve propVars mapping
    RuntimeFn->>RuntimeFn: Process each prop using runtimeFn.props method
    alt CSS variable found
        RuntimeFn->>RuntimeFn: Map prop value to corresponding CSS variable
    else No variable match
        RuntimeFn->>RuntimeFn: Skip property
    end
    RuntimeFn-->>Client: Return constructed CSSRule object
Loading

Possibly related PRs

  • mincho-js/mincho#110: Updates to the createRuntimeFn function’s type parameters and internal logic are closely related.
  • mincho-js/mincho#132: Enhancements in property type definitions in createRuntimeFn mirror updates in this PR.
  • mincho-js/mincho#99: Similar modifications enhancing the RuntimeFn type and its usage in CSS rule generation.

Suggested labels

dependencies

Poem

In my burrow of code, I hop with delight,
Nibbling on changes both clever and bright.
CSS rules now twirl with a magical hue,
As variables dance and the types all renew.
A happy rabbit sings—may these updates delight!
🐇✨


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.

@github-actions
Copy link
Contributor

Triggered from #150 by @​black7375.

Checking if we can fast forward main (a277d8b) to runtime-props (afb191f).

Target branch (main):

commit a277d8b52a1c50b847c08eefb06345e3de238509 (HEAD -> main, origin/main, origin/HEAD)
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Feb 10 01:10:20 2025 +0000

    Chore(deps-dev): Bump the dev-dependencies group with 3 updates
    
    Bumps the dev-dependencies group with 3 updates: [prettier](https://github.com/prettier/prettier), [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) and [eslint-plugin-react-refresh](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
    
    
    Updates `prettier` from 3.4.2 to 3.5.0
    - [Release notes](https://github.com/prettier/prettier/releases)
    - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/prettier/prettier/compare/3.4.2...3.5.0)
    
    Updates `@vitejs/plugin-react-swc` from 3.7.2 to 3.8.0
    - [Release notes](https://github.com/vitejs/vite-plugin-react-swc/releases)
    - [Changelog](https://github.com/vitejs/vite-plugin-react-swc/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/vitejs/vite-plugin-react-swc/compare/v3.7.2...v3.8.0)
    
    Updates `eslint-plugin-react-refresh` from 0.4.18 to 0.4.19
    - [Release notes](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/releases)
    - [Changelog](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/compare/v0.4.18...v0.4.19)
    
    ---
    updated-dependencies:
    - dependency-name: prettier
      dependency-type: direct:development
      update-type: version-update:semver-minor
      dependency-group: dev-dependencies
    - dependency-name: "@vitejs/plugin-react-swc"
      dependency-type: direct:development
      update-type: version-update:semver-minor
      dependency-group: dev-dependencies
    - dependency-name: eslint-plugin-react-refresh
      dependency-type: direct:development
      update-type: version-update:semver-patch
      dependency-group: dev-dependencies
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>

Pull request (runtime-props):

commit afb191ffb212fa21044fa5b5b746afef2e2f4488 (pull_request/runtime-props)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Mon Feb 10 01:28:58 2025 +0900

    Feat: `rules()` props runtime #131

It is possible to fast forward main (a277d8b) to runtime-props (afb191f). If you have write access to the target repository, you can add a comment with /fast-forward to fast forward main to runtime-props.

@github-actions
Copy link
Contributor

Triggered from #150 by @​black7375.

Checking if we can fast forward main (a277d8b) to runtime-props (7899606).

Target branch (main):

commit a277d8b52a1c50b847c08eefb06345e3de238509 (HEAD -> main, origin/main, origin/HEAD)
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Feb 10 01:10:20 2025 +0000

    Chore(deps-dev): Bump the dev-dependencies group with 3 updates
    
    Bumps the dev-dependencies group with 3 updates: [prettier](https://github.com/prettier/prettier), [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) and [eslint-plugin-react-refresh](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
    
    
    Updates `prettier` from 3.4.2 to 3.5.0
    - [Release notes](https://github.com/prettier/prettier/releases)
    - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/prettier/prettier/compare/3.4.2...3.5.0)
    
    Updates `@vitejs/plugin-react-swc` from 3.7.2 to 3.8.0
    - [Release notes](https://github.com/vitejs/vite-plugin-react-swc/releases)
    - [Changelog](https://github.com/vitejs/vite-plugin-react-swc/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/vitejs/vite-plugin-react-swc/compare/v3.7.2...v3.8.0)
    
    Updates `eslint-plugin-react-refresh` from 0.4.18 to 0.4.19
    - [Release notes](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/releases)
    - [Changelog](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/compare/v0.4.18...v0.4.19)
    
    ---
    updated-dependencies:
    - dependency-name: prettier
      dependency-type: direct:development
      update-type: version-update:semver-minor
      dependency-group: dev-dependencies
    - dependency-name: "@vitejs/plugin-react-swc"
      dependency-type: direct:development
      update-type: version-update:semver-minor
      dependency-group: dev-dependencies
    - dependency-name: eslint-plugin-react-refresh
      dependency-type: direct:development
      update-type: version-update:semver-patch
      dependency-group: dev-dependencies
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>

Pull request (runtime-props):

commit 7899606780aa81a3e6efd5dacfe4184ba9713c7e (pull_request/runtime-props)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Mon Feb 10 01:33:02 2025 +0900

    Feat: Rules - `props()` runtime #131

It is possible to fast forward main (a277d8b) to runtime-props (7899606). If you have write access to the target repository, you can add a comment with /fast-forward to fast forward main to runtime-props.

Copy link

@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 (5)
packages/css/src/rules/createRuntimeFn.ts (1)

78-90: Confirm behavior of runtimeFn.props with various prop value types.

By assigning result[varName] = propValue as string;, values like numbers or booleans are coerced to strings without explicit checking. If numeric or boolean prop values are expected, consider converting them with String(propValue) to avoid confusion. Otherwise, ensure that only string-compatible values are passed to maintain type consistency.

packages/css/src/rules/types.ts (1)

70-71: ComplexPropDefinitions usability check.

Allowing an array of PropKeys | PropDefinition<PropKeys> is flexible, but increases complexity. Consider documenting the expected shape for arrays containing mixed string and object definitions for future maintainability.

packages/css/src/utils.ts (2)

11-23: Add input validation to enhance robustness.

While the implementation is optimized and handles all cases correctly, it could benefit from input validation.

Consider adding input validation:

 export function getVarName(variable: string): PureCSSVarKey {
+  if (typeof variable !== 'string') {
+    throw new TypeError('Variable must be a string');
+  }
   if (variable.startsWith("var(") && variable.endsWith(")")) {
     const inside = variable.slice(VAR_PREFIX_LENGTH, -1);
     const commaIndex = inside.indexOf(",");
     return (
       commaIndex === -1 ? inside : inside.slice(0, commaIndex)
     ) as PureCSSVarKey;
   }
   return variable as PureCSSVarKey;
 }

36-62: Add edge case tests for improved coverage.

The test suite is comprehensive but could benefit from additional edge cases.

Consider adding these test cases:

 describe("getVarName", () => {
+  it("edge cases", () => {
+    expect(() => getVarName(null as any)).toThrow(TypeError);
+    expect(() => getVarName(undefined as any)).toThrow(TypeError);
+    expect(getVarName("var()")).toBe("var()");
+    expect(getVarName("var(--name,)")).toBe("--name");
+  });

   it("string", () => {
     // existing tests...
packages/transform-to-vanilla/src/types/style-rule.ts (1)

129-140: Enhance type safety for CSS variable functions.

The type definitions are well-structured, but the PureCSSVarFunction type could be more precise.

Consider this improvement:

-export type PureCSSVarFunction =
-  | `var(--${string})`
-  | `var(--${string}, ${CSSVarValue})`;
+export type PureCSSVarKey = `--${string}`;
+export type PureCSSVarFunction =
+  | `var(${PureCSSVarKey})`
+  | `var(${PureCSSVarKey}, ${CSSVarValue})`;
 export type CSSVarFunction =
   | PureCSSVarFunction
   | `$${string}`
   | `$${string}(${CSSVarValue})`;

This change:

  1. Reuses the PureCSSVarKey type for better consistency
  2. Makes the relationship between variable keys and functions more explicit
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a277d8b and afb191f.

📒 Files selected for processing (5)
  • packages/css/src/rules/createRuntimeFn.ts (3 hunks)
  • packages/css/src/rules/index.ts (12 hunks)
  • packages/css/src/rules/types.ts (7 hunks)
  • packages/css/src/utils.ts (1 hunks)
  • packages/transform-to-vanilla/src/types/style-rule.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (16)
packages/css/src/rules/createRuntimeFn.ts (2)

1-11: Ensure consistent type usage in imports.

The new imports and type references look consistent and correct. No issues found regarding correctness or security.


29-35: Validate new generic parameter addition.

The updated function signature now handles two generic parameters <Variants, Props>, providing greater extensibility for property definitions. Ensure any external calls to createRuntimeFn are updated accordingly to supply the second generic parameter when necessary (or allow it to infer, if intended).

packages/css/src/rules/types.ts (5)

2-9: Imports appear consistent.

Bringing in PureCSSVarKey and other types from @mincho-js/transform-to-vanilla aligns well with the extended property definitions.


93-111: Optional property handling in HandlePropTarget and PropDefinitionOutputElement.

Making target properties optional (e.g., using ?) can help with type safety but may cause partial definitions to pass inadvertently. Verify that downstream usage handles the absence of these properties gracefully.


117-128: Enhanced property variable mappings with PropVars.

Exposing propVars: PropVars<Props> in PatternResult is a robust approach for referencing CSS variable names. Ensure code outside this module enforces the correct mapping of property names to these variables to avoid runtime confusion.


168-172: RuntimeFn signature consolidation.

Merging a callable function with an object that includes .props, .variants, and .classNames offers a complete interface. Double-check that all usage sites handle both function invocation and property access patterns consistently.


177-189: RulesVariants & RecipeVariants expansions.

The updated utility types that incorporate property definitions maintain strong alignment with the rest of the codebase. These expansions appear correct. No immediate concerns.

packages/css/src/rules/index.ts (8)

2-12: Imports and utility references.

Importing createVar, fallbackVar, and referencing className / getVarName extends the library’s functionality for property-based variable generation. Implementation looks cohesive.


45-48: Return type improvement.

Returning RuntimeFn<ConditionalVariants<Variants, ToggleVariants>, Exclude<Props, undefined>> explicitly clarifies the presence of props in the final runtime function. Verify any external calls expecting the old return signature handle the new pattern properly.


54-55: Explicit grouping of base and props.

Separating props from other style definitions in the destructuring clarifies the boundary between standard styling and property-based variables. This is a readable and maintainable approach.


59-77: Initialize and populate propVars & propStyles.

Good approach for simultaneously defining the variable mappings (propVars) and referencing them in propStyles. However, ensure that properties initial setup remains consistent if new properties are added dynamically at runtime.


78-90: Base class composition with propStyles.

Lines 80-89 handle merging base, baseStyles, and propStyles. This approach effectively consolidates the final base class name. No immediate issues spotted.


126-132: Inclusion of propVars in PatternResult.

Storing propVars in the config object is consistent with other pattern aspects (variantClassNames, compoundVariants). This ensures all property-based variables remain accessible to the runtime function.


150-167: processPropObject for structured prop definitions.

This helper systematically creates variables for each key in the prop definition and emits fallback references to a base value if provided. Using fallbackVar ensures graceful handling of missing or undefined properties.


605-716: Test coverage for props scenario.

The test cases thoroughly verify partial vs. full props usage, ensuring extraneous keys are ignored. This coverage is excellent and should be maintained for additional property scenarios.

packages/css/src/utils.ts (1)

1-3: LGTM! Clean and focused imports.

The imports are well-organized and properly scoped to the required functionality.

@black7375
Copy link
Contributor Author

/fast-forward

@github-actions
Copy link
Contributor

Triggered from #150 (comment) by @​black7375.

Trying to fast forward main (a277d8b) to runtime-props (7899606).

Target branch (main):

commit a277d8b52a1c50b847c08eefb06345e3de238509 (HEAD -> main, origin/main, origin/HEAD)
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Feb 10 01:10:20 2025 +0000

    Chore(deps-dev): Bump the dev-dependencies group with 3 updates
    
    Bumps the dev-dependencies group with 3 updates: [prettier](https://github.com/prettier/prettier), [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) and [eslint-plugin-react-refresh](https://github.com/ArnaudBarre/eslint-plugin-react-refresh).
    
    
    Updates `prettier` from 3.4.2 to 3.5.0
    - [Release notes](https://github.com/prettier/prettier/releases)
    - [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/prettier/prettier/compare/3.4.2...3.5.0)
    
    Updates `@vitejs/plugin-react-swc` from 3.7.2 to 3.8.0
    - [Release notes](https://github.com/vitejs/vite-plugin-react-swc/releases)
    - [Changelog](https://github.com/vitejs/vite-plugin-react-swc/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/vitejs/vite-plugin-react-swc/compare/v3.7.2...v3.8.0)
    
    Updates `eslint-plugin-react-refresh` from 0.4.18 to 0.4.19
    - [Release notes](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/releases)
    - [Changelog](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/blob/main/CHANGELOG.md)
    - [Commits](https://github.com/ArnaudBarre/eslint-plugin-react-refresh/compare/v0.4.18...v0.4.19)
    
    ---
    updated-dependencies:
    - dependency-name: prettier
      dependency-type: direct:development
      update-type: version-update:semver-minor
      dependency-group: dev-dependencies
    - dependency-name: "@vitejs/plugin-react-swc"
      dependency-type: direct:development
      update-type: version-update:semver-minor
      dependency-group: dev-dependencies
    - dependency-name: eslint-plugin-react-refresh
      dependency-type: direct:development
      update-type: version-update:semver-patch
      dependency-group: dev-dependencies
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>

Pull request (runtime-props):

commit 7899606780aa81a3e6efd5dacfe4184ba9713c7e (pull_request/runtime-props)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Mon Feb 10 01:33:02 2025 +0900

    Feat: Rules - `props()` runtime #131

Fast forwarding main (a277d8b) to runtime-props (7899606).

$ git push origin 7899606780aa81a3e6efd5dacfe4184ba9713c7e:main
To https://github.com/mincho-js/mincho.git
   a277d8b..7899606  7899606780aa81a3e6efd5dacfe4184ba9713c7e -> main

@github-actions github-actions bot merged commit 7899606 into main Feb 10, 2025
7 checks passed
@github-actions github-actions bot deleted the runtime-props branch February 10, 2025 16:37
@black7375 black7375 linked an issue Feb 10, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Props: Dynamic style

1 participant