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

fix(icon): solve the style problem caused by multiple identical ids o… #2086

Merged
merged 3 commits into from
Sep 9, 2024

Conversation

zzcr
Copy link
Member

@zzcr zzcr commented Sep 9, 2024

…f gradient svg

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Enhanced SVG handling capabilities with improved import and optimization processes for both Vue 2 and Vue 3.
    • Introduced new modules for a mobile-first approach and hooks for better state management.
    • Added functionality to generate unique identifiers and URLs for SVG elements, ensuring proper rendering.
  • Bug Fixes

    • Resolved ID conflicts in SVG icons to prevent styling issues when multiple icons share the same ID.

@zzcr zzcr added the bug Something isn't working label Sep 9, 2024
Copy link

coderabbitai bot commented Sep 9, 2024

Walkthrough

The pull request introduces enhancements to SVG handling in both Vue 2 and Vue 3 configurations within the vite.config.ts files. It updates the SVG plugins to include detailed optimization configurations, ensuring the preservation of essential attributes and preventing ID conflicts. Additionally, new modules are added to the modules.json file, and a new utility file for generating unique identifiers and URLs for SVG elements is introduced, improving the overall management of SVG graphics in the application.

Changes

Files Change Summary
examples/sites/vite.config.ts Updated vue3SvgPlugin to include defaultImport and svgoConfig for improved SVG handling.
examples/vue2/vite.config.ts Enhanced vue2SvgPlugin with svgoConfig to optimize SVGs, preserving viewBox and avoiding ID conflicts.
packages/modules.json Added new entries for "AnchorMobileFirst" and "Hooks" modules, indicating a mobile-first approach and the inclusion of hooks for state management.
packages/vue-common/src/generateIcon.ts Introduced functionality for generating unique IDs and URLs for SVG elements, ensuring proper handling of identifiers and references in virtual nodes.
packages/vue-common/src/index.ts Added logic to manage SVG icons with gradients, ensuring unique ID mappings to prevent styling issues during rendering.

Possibly related PRs

🐇 In fields of green, the changes bloom,
With SVGs now dancing, dispelling the gloom.
Unique IDs hop, like bunnies in spring,
Optimized icons, oh what joy they bring!
A mobile-first leap, with hooks to embrace,
In CodeRabbit's world, we quicken the pace! 🌼

Tip

New features

Walkthrough comment now includes:

  • Possibly related PRs: A list of potentially related PRs to help you recall past context.
  • Suggested labels: CodeRabbit can now suggest labels by learning from your past PRs. You can also provide custom labeling instructions in the UI or configuration file.

Notes:

  • Please share any feedback in the discussion post on our Discord.
  • Possibly related PRs, automatic label suggestions based on past PRs, learnings, and possibly related issues require data opt-in (enabled by default).

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 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

github-actions bot commented Sep 9, 2024

[e2e-test-warn]
The component to be tested is missing.

The title of the Pull request should look like "fix(vue-renderless): [action-menu, alert] fix xxx bug".

Please make sure you've read our contributing guide

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: 2

Outside diff range, codebase verification and nitpick comments (1)
packages/vue-common/src/generateIcon.ts (1)

36-43: Consider encapsulating the uniqueId to avoid concurrency issues.

The global uniqueId used in generateIcon could lead to issues in concurrent scenarios where multiple instances might interfere with each other. Consider encapsulating this state within a class or using a more robust method to generate unique IDs that are safe in concurrent environments.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 363e730 and 2f2b343.

Files selected for processing (6)
  • examples/sites/vite.config.ts (1 hunks)
  • examples/vue2/vite.config.ts (1 hunks)
  • examples/vue3/vite.config.ts (1 hunks)
  • packages/modules.json (2 hunks)
  • packages/vue-common/src/generateIcon.ts (1 hunks)
  • packages/vue-common/src/index.ts (2 hunks)
Additional comments not posted (5)
examples/vue2/vite.config.ts (1)

32-46: Approve the enhanced SVG handling configuration.

The updated configuration for vue2SvgPlugin is well-thought-out, addressing common issues with SVGs in web projects by ensuring the viewBox is preserved and IDs are prefixed. This should effectively prevent conflicts and improve the handling of SVGs across the application.

examples/vue3/vite.config.ts (1)

39-54: Approve the updated SVG plugin configuration.

The configuration changes to vue3SvgPlugin are appropriate and align with best practices for handling SVGs in modern web applications. Setting the default import to 'component' and configuring SVG optimization plugins enhances the flexibility and usability of SVGs within the project.

examples/sites/vite.config.ts (1)

49-64: Approved: Enhanced SVG handling in Vite configuration.

The configuration changes for vue3SvgPlugin are correctly implemented to improve SVG handling:

  • SVGs are now imported as components, which is a best practice in Vue 3.
  • The svgoConfig is properly set to retain the viewBox and prefix IDs, addressing the PR's goal of solving style issues caused by multiple identical IDs.

Consider verifying the rendering of SVGs in the application to ensure that there are no visual regressions or issues due to these configuration changes.

packages/vue-common/src/index.ts (1)

28-28: Approved: Enhancements to SVG icon handling in Vue components.

The introduction of GRADIENT_ICONS_LIST and generateIcon is well-implemented to address the issue of duplicate SVG IDs:

  • The logic to check and modify SVG IDs dynamically is a robust solution to prevent style conflicts.
  • The changes ensure compatibility with Vue 2 by correctly binding and modifying the render function.

It's recommended to add unit tests to verify that SVG icons with gradients are rendered correctly without ID conflicts or other styling issues.

Also applies to: 286-299

packages/modules.json (1)

87-91: Approved: New module definitions for AnchorMobileFirst and Hooks.

The additions of AnchorMobileFirst and Hooks modules are correctly configured:

  • AnchorMobileFirst supports the mobile-first design strategy.
  • Hooks module likely provides reusable state management functions, enhancing modularity.

Ensure that documentation is updated to include information on the roles and usage of these new modules, helping developers understand their integration and purpose in the project.

Also applies to: 1473-1477

Comment on lines 4 to 15
const generateId = (vnode, idMaps) => {
if (vnode.props?.id) {
const newId = `${vnode.props.id}${uniqueId}`
idMaps[vnode.props.id] = newId
vnode.props.id = newId
}

if (vnode?.children) {
vnode?.children.forEach((item) => {
generateId(item, idMaps)
})
}
Copy link

Choose a reason for hiding this comment

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

Document the side effects of modifying input objects.

The generateId function modifies the vnode object and its children directly by changing their id properties. It's important to document these side effects clearly in the function's documentation to avoid unexpected behavior for users of this function.

Would you like me to help draft the documentation for this function?

Comment on lines 18 to 33
const generateUrl = (vnode, idMaps) => {
const checkList = ['fill', 'mask']

checkList.forEach((item) => {
if (vnode.props?.[item]?.includes('url(#')) {
const oldId = vnode.props[item].replace('url(#', '').replace(')', '')
const newId = idMaps[oldId]
vnode.props[item] = `url(#${newId})`
}
})

if (vnode.children) {
vnode.children.forEach((item) => {
generateUrl(item, idMaps)
})
}
Copy link

Choose a reason for hiding this comment

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

Enhance robustness in URL handling.

The generateUrl function assumes a specific format when extracting and replacing IDs in URLs. Consider using a more robust method to handle different possible URL formats, such as using regular expressions to match and replace the ID part of the URL.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2f2b343 and a26fc25.

Files selected for processing (2)
  • packages/vue-common/src/generateIcon.ts (1 hunks)
  • packages/vue-common/src/index.ts (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/vue-common/src/generateIcon.ts
  • packages/vue-common/src/index.ts

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a26fc25 and 26467e1.

Files selected for processing (1)
  • packages/vue-common/src/generateIcon.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/vue-common/src/generateIcon.ts

@kagol kagol merged commit 8b743d0 into dev Sep 9, 2024
6 checks passed
@kagol kagol deleted the fix-svg-ids-bug-0909 branch November 4, 2024 03:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants