Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

fix: When ce is used multiple times, subcomponents can mount the style #60

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/sub-style/src/inject/inject-api-custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@ const injectBindFnContent
= 'instance.addCEChildStyle = this._addChildStyles.bind(this);\n'
+ ' instance.removeCEChildStyle = this._removeChildStyles.bind(this);\n '

const injectAddAndRemoveStyle = ' _addChildStyles(styles, instance) {\n'
const injectAddAndRemoveStyle = '_addChildStyles(styles, instance) {\n'
+ ' if (styles) {\n'
+ ' const styleContent = styles.join();\n'
+ ' let cecStyle = /* @__PURE__ */ new Set();\n'
// eslint-disable-next-line no-template-curly-in-string
+ ' const ceKey = `__${this._instance.uid}`;\n'
+ ' let ceKeySet = /* @__PURE__ */ new Set();\n'
+ ' if (ceChildStyleMap.has(styleContent)) {\n'
+ ' cecStyle = ceChildStyleMap.get(styleContent);\n'
+ ' cecStyle.add(instance.uid);\n'
+ ' ceChildStyleMap.set(styleContent, cecStyle);\n'
+ ' return;\n'
+ ' ceKeySet = ceChildStyleMap.get(styleContent);\n'
+ ' if (ceKeySet.has(ceKey)) {\n'
+ ' ceKeySet.add(ceKey);\n'
+ ' ceChildStyleMap.set(styleContent, ceKeySet);\n'
+ ' return;\n'
+ ' }\n'
+ ' }\n'
+ ' cecStyle.add(instance.uid);\n'
+ ' ceChildStyleMap.set(styleContent, cecStyle);\n'
+ ' ceKeySet.add(ceKey);\n'
+ ' ceChildStyleMap.set(styleContent, ceKeySet);\n'
// eslint-disable-next-line no-template-curly-in-string
+ ' const ceStyleId = `data-v-ce-${instance.uid}`;\n'
+ ' styles.forEach((css, index) => {\n'
Expand All @@ -52,8 +56,10 @@ const injectAddAndRemoveStyle = ' _addChildStyles(styles, instance) {\n'
+ ' const styleContent = styles.join();\n'
+ ' let cecStyle = /* @__PURE__ */ new Set();\n'
+ ' if (ceChildStyleMap.has(styleContent)) {\n'
// eslint-disable-next-line no-template-curly-in-string
+ ' const ceKey = `__${this._instance.uid}`;\n'
+ ' cecStyle = ceChildStyleMap.get(styleContent);\n'
+ ' cecStyle.delete(uid);\n'
+ ' cecStyle.delete(ceKey);\n'
+ ' if (cecStyle.size === 0) {\n'
// eslint-disable-next-line no-template-curly-in-string
+ ' const sList = this.shadowRoot.querySelectorAll(`[data-v-ce-${uid}]`);\n'
Expand Down