Skip to content

Fix: Global CSS using hoisting selectors#209

Merged
github-actions[bot] merged 1 commit intomainfrom
fix-global-css
Jun 6, 2025
Merged

Fix: Global CSS using hoisting selectors#209
github-actions[bot] merged 1 commit intomainfrom
fix-global-css

Conversation

@black7375
Copy link
Contributor

@black7375 black7375 commented Jun 6, 2025

Description

  1. Global style allow nested selector using hoisting
  2. Add VariantStyle type
  3. Fix vitest workspace setup

Related Issue

Summary by CodeRabbit

  • New Features

    • Enhanced global CSS styling capabilities with support for nested selectors.
    • Introduced a new type for more structured variant styles in styling definitions.
  • Chores

    • Updated test configuration to use a new Vitest setup for managing multiple projects.
    • Removed obsolete Vitest workspace configuration.

Additional context

Checklist

@changeset-bot
Copy link

changeset-bot bot commented Jun 6, 2025

🦋 Changeset detected

Latest commit: df91f14

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

This PR includes changesets to release 4 packages
Name Type
@mincho-js/css Patch
@mincho-js/react Patch
@mincho-js/vite Patch
react-babel Patch

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

@coderabbitai
Copy link

coderabbitai bot commented Jun 6, 2025

Walkthrough

This update introduces a new VariantStyle type for structured variant styling and refactors the globalCss function to support nested selectors and at-rule handling. It also adjusts test type assertions, updates exports, and replaces the Vitest workspace configuration with a new project-based setup.

Changes

Files/Paths Change Summary
packages/css/src/rules/types.ts Added new generic type alias VariantStyle for mapping variant names to CSS rules.
packages/css/src/index.ts Exported the new VariantStyle type.
packages/css/src/css/index.ts Refactored globalCss to support nested selectors and at-rules; added hoistSelectors helper.
packages/css/src/rules/index.ts Enforced stricter type assertions using VariantStyle in test code.
.changeset/sharp-ears-wear.md Added changeset summary for the introduction of VariantStyle and nested selector support.
vitest.config.ts Introduced new Vitest config using project-based testing.
vitest.workspace.ts Deleted old Vitest workspace configuration.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant globalCss
    participant hoistSelectors
    participant gStyle

    User->>globalCss: Call with selector and rule
    globalCss->>globalCss: Wrap rule in selectors object
    globalCss->>hoistSelectors: Hoist nested selectors/at-rules
    hoistSelectors-->>globalCss: Return flattened selectors and at-rules
    loop For each selector
        globalCss->>gStyle: Apply style for selector
    end
    loop For each at-rule
        globalCss->>gStyle: Apply style for at-rule/selector
    end
Loading

Possibly related PRs

Suggested labels

enhancement, bug

Poem

In the garden of code where the variants grow,
A new style emerges, with selectors in tow.
Nested and tidy, the CSS now sings,
While Vitest hops forward on project-based wings.
🐇✨

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.

yarn install v1.22.22
[1/4] Resolving packages...
(node:20694) [DEP0169] DeprecationWarning: url.parse() behavior is not standardized and prone to errors that have security implications. Use the WHATWG URL API instead. CVEs are not issued for url.parse() vulnerabilities.
(Use node --trace-deprecation ... to show where the warning was created)
error Couldn't find any versions for "eslint-config-custom" that matches "workspace:^"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

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

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2025

Triggered from #209 by @​black7375.

Checking if we can fast forward main (2a6fe64) to fix-global-css (df91f14).

Target branch (main):

commit 2a6fe6490b7c37743c4f5afe3c75de25feff7e85 (HEAD -> main, origin/main)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Fri May 9 00:00:00 2025 +0900

    Chore: fix security issues

Pull request (fix-global-css):

commit df91f148cf4c0db6a19f544fb993943cc1f50a70 (pull_request/fix-global-css)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Sat Jun 7 03:32:43 2025 +0900

    Fix: Nested Global CSS using hoisting selectors

It is possible to fast forward main (2a6fe64) to fix-global-css (df91f14). If you have write access to the target repository, you can add a comment with /fast-forward to fast forward main to fix-global-css.

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 (3)
vitest.config.ts (1)

3-7: Consider adding common test configuration options.

While the minimal configuration is appropriate for a project-based setup, you might want to consider adding common test options that apply across all projects, such as:

 export default defineConfig({
   test: {
     projects: ["packages/*/vite.config.ts"],
+    globals: true,
+    environment: "happy-dom"
   }
 });

This ensures consistent test environments across all packages while still allowing individual packages to override these settings in their own vite.config.ts files.

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

43-48: Address the TODO: Improve type safety

The UnknownObject type using Record<string, unknown> sacrifices type safety. Consider defining more specific types based on the actual structure of CSS rules and at-rules to catch potential issues at compile time.

Would you like me to help create more type-safe definitions based on the expected structure of the transformed CSS objects?


50-102: Consider adding validation and improving type safety in hoistSelectors

The implementation correctly hoists selectors, but could benefit from:

  1. Path validation: The function assumes path contains pairs of (atRule, condition), but doesn't validate this assumption
  2. Property collision handling: Object.assign on line 83 could silently overwrite existing properties

Consider adding validation:

 function processAtRules(obj: UnknownObject, path: string[] = []) {
+  // Ensure path has even length (pairs of atRule/condition)
+  if (path.length % 2 !== 0) {
+    throw new Error('Invalid path structure: expected pairs of atRule/condition');
+  }
+
   for (const key in obj) {
     if (key === "selectors") {

For property collisions, consider using a merge strategy or warning when overwriting:

-          Object.assign(current, selectors[selector]);
+          // Warn about property collisions during development
+          if (process.env.NODE_ENV !== 'production') {
+            const existingKeys = Object.keys(current);
+            const newKeys = Object.keys(selectors[selector]);
+            const collisions = existingKeys.filter(k => newKeys.includes(k));
+            if (collisions.length > 0) {
+              console.warn(`Property collision in hoisted selectors: ${collisions.join(', ')}`);
+            }
+          }
+          Object.assign(current, selectors[selector]);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2a6fe64 and df91f14.

📒 Files selected for processing (7)
  • .changeset/sharp-ears-wear.md (1 hunks)
  • packages/css/src/css/index.ts (1 hunks)
  • packages/css/src/index.ts (1 hunks)
  • packages/css/src/rules/index.ts (2 hunks)
  • packages/css/src/rules/types.ts (1 hunks)
  • vitest.config.ts (1 hunks)
  • vitest.workspace.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • vitest.workspace.ts
🧰 Additional context used
🧠 Learnings (1)
packages/css/src/index.ts (1)
Learnt from: black7375
PR: mincho-js/mincho#113
File: packages/css/src/index.ts:33-33
Timestamp: 2024-10-09T13:00:38.449Z
Learning: `recipe` is properly exported from './rules' in `packages/css/src/rules/index.ts`.
🔇 Additional comments (9)
vitest.config.ts (2)

1-2: LGTM!

The import statement correctly uses defineConfig from vitest/config, which is the standard approach for TypeScript-based Vitest configurations.


3-7:

Details

✅ Verification successful

Verify that all packages have vite.config.ts files.

The configuration structure looks good and the move from workspace to project-based configuration is a solid architectural decision. However, ensure that all packages in the packages/* directories actually have vite.config.ts files, as missing files could cause configuration errors.


🏁 Script executed:

#!/bin/bash
# Description: Verify that all packages have vite.config.ts files as expected by the Vitest configuration.
# Expected: All directories in packages/ should have vite.config.ts files.

echo "Checking for packages directories:"
fd -t d . packages --max-depth 1

echo -e "\nChecking for vite.config.ts files in packages:"
fd "vite.config.ts" packages

echo -e "\nPackages without vite.config.ts files:"
for dir in packages/*/; do
  if [ -d "$dir" ] && [ ! -f "${dir}vite.config.ts" ]; then
    echo "Missing: ${dir}vite.config.ts"
  fi
done

Length of output: 1823


All packages have vite.config.ts files as expected

Verified that every directory under packages/ contains a vite.config.ts file, so the Vitest projects: ["packages/*/vite.config.ts"] pattern is safe. No further changes needed—approving this configuration.

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

39-39: LGTM! Proper type export for public API.

The export of VariantStyle type follows the established pattern and makes the new type available for external usage, which aligns with the type safety improvements mentioned in the changeset.

.changeset/sharp-ears-wear.md (1)

8-9: Verify completeness of the changeset documentation.

The changeset mentions changes to the globalCss function to allow nested selectors, but these changes are not visible in the provided files for review. This could indicate incomplete file coverage in the PR.

Please verify that all relevant files mentioned in the changeset are included in the PR, particularly the globalCss function changes referenced in packages/css/src/css/index.ts.

Likely an incorrect or invalid review comment.

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

29-30: LGTM! Proper import of new types.

The addition of VariantStringMap and VariantStyle imports supports the enhanced type checking implemented in the test cases below.


268-268: Excellent use of satisfies operator for type safety.

The type assertions using satisfies VariantStyle<...> provide compile-time type checking for variant objects without affecting runtime behavior. This ensures the test data conforms to the expected variant structure and helps catch type mismatches early.

Also applies to: 273-273, 277-277

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

34-39: Well-designed generic type for variant constraints.

The VariantStyle type is properly structured with:

  • Generic constraint VariantNames extends string for flexibility
  • Sensible default CssRule extends RecipeStyleRule = RecipeStyleRule
  • Clear mapped type structure mapping variant names to CSS rules

This enhances type safety for variant style definitions across the codebase.


41-41: Helpful clarifying comment.

The comment about VariantDefinitions being similar to VariantMap but optimized for fast type checking provides valuable context for maintainers.

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

18-40: Well-implemented refactoring to support nested selectors!

The restructuring of globalCss to wrap rules in a selectors object and separately handle at-rule styles is a clean approach. This enables the nested selector support mentioned in the PR objectives.

@black7375
Copy link
Contributor Author

/fast-forward

@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2025

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

Trying to fast forward main (2a6fe64) to fix-global-css (df91f14).

Target branch (main):

commit 2a6fe6490b7c37743c4f5afe3c75de25feff7e85 (HEAD -> main, origin/main)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Fri May 9 00:00:00 2025 +0900

    Chore: fix security issues

Pull request (fix-global-css):

commit df91f148cf4c0db6a19f544fb993943cc1f50a70 (pull_request/fix-global-css)
Author: alstjr7375 <alstjr7375@daum.net>
Date:   Sat Jun 7 03:32:43 2025 +0900

    Fix: Nested Global CSS using hoisting selectors

Fast forwarding main (2a6fe64) to fix-global-css (df91f14).

$ git push origin df91f148cf4c0db6a19f544fb993943cc1f50a70:main
To https://github.com/mincho-js/mincho.git
   2a6fe64..df91f14  df91f148cf4c0db6a19f544fb993943cc1f50a70 -> main

@github-actions github-actions bot merged commit df91f14 into main Jun 6, 2025
10 checks passed
@github-actions github-actions bot deleted the fix-global-css branch June 6, 2025 18:37
@github-actions github-actions bot mentioned this pull request Jun 6, 2025
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.

1 participant