Skip to content

Commit 49b72ee

Browse files
committed
refactor: use CSSStyleSheet object to change the custom property of size inside shadow DOM dynamically
1 parent 7c56c38 commit 49b72ee

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/main.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ template.innerHTML = templateContent
2525

2626
const PLACEMENT = ['top-right', 'top-left', 'bottom-right', 'bottom-left']
2727

28-
const DEFAULT_SIZE = '5rem'
2928
const DEFAULT_BANNER_COLOR = 'black'
3029
const DEFAULT_OCTOCAT_COLOR = 'white'
3130
const DEFAULT_DURATION = '0.5s'
3231

3332
class GithubCorner extends HTMLElement {
3433
#shadowRoot: ShadowRoot
34+
#styleSheet: CSSStyleSheet
3535

3636
#anchor: HTMLAnchorElement
3737
#svgContainer: SVGElement
@@ -46,14 +46,15 @@ class GithubCorner extends HTMLElement {
4646
constructor() {
4747
super()
4848

49+
this.#styleSheet = new CSSStyleSheet()
50+
this.#styleSheet.replaceSync(cssContent)
51+
4952
// Create a shadow root
5053
this.#shadowRoot = this.attachShadow({ mode: 'open' })
51-
52-
const style = document.createElement('style')
53-
style.textContent = cssContent
54+
this.#shadowRoot.adoptedStyleSheets = [this.#styleSheet]
5455

5556
// attach the created elements to the shadow DOM
56-
this.#shadowRoot.append(style, template.content.cloneNode(true))
57+
this.#shadowRoot.append(template.content.cloneNode(true))
5758
this.#anchor = this.#shadowRoot.querySelector('a.link') as HTMLAnchorElement
5859

5960
this.#svgContainer = this.#shadowRoot.querySelector(
@@ -91,9 +92,6 @@ class GithubCorner extends HTMLElement {
9192
}
9293

9394
#init() {
94-
const size = this.getAttribute('size')
95-
this.#setSize(size ? size : DEFAULT_SIZE)
96-
9795
if (!this.#banner.getAttribute('fill')) {
9896
this.#banner.setAttribute('fill', DEFAULT_BANNER_COLOR)
9997
}
@@ -110,11 +108,6 @@ class GithubCorner extends HTMLElement {
110108
}
111109
}
112110

113-
#setSize(size: string) {
114-
this.style.width = size
115-
this.style.height = size
116-
}
117-
118111
connectedCallback() {
119112
if (this.#status === 'init') {
120113
this.#init()
@@ -137,7 +130,11 @@ class GithubCorner extends HTMLElement {
137130
) {
138131
switch (attributeName) {
139132
case 'size':
140-
this.#setSize(newValue)
133+
// replace size custom property
134+
this.#styleSheet.deleteRule(0)
135+
this.#styleSheet.insertRule(
136+
`:host { --github-corner-size: ${newValue}; }`
137+
)
141138
break
142139

143140
case 'href':

src/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
:host {
2+
--github-corner-size: 5rem;
3+
}
4+
15
:host {
26
display: block;
37
position: absolute;
48
cursor: pointer;
59
overflow: hidden;
610
clip-path: polygon(0% 0%, 100% 0%, 100% 100%);
11+
width: var(--github-corner-size);
12+
height: var(--github-corner-size);
713
}
814

915
:host([placement='top-left']) {

0 commit comments

Comments
 (0)