Skip to content
Draft
Show file tree
Hide file tree
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
25 changes: 11 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ template.innerHTML = templateContent

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

const DEFAULT_SIZE = '5rem'
const DEFAULT_BANNER_COLOR = 'black'
const DEFAULT_OCTOCAT_COLOR = 'white'
const DEFAULT_DURATION = '0.5s'

class GithubCorner extends HTMLElement {
#shadowRoot: ShadowRoot
#styleSheet: CSSStyleSheet

#anchor: HTMLAnchorElement
#svgContainer: SVGElement
Expand All @@ -46,14 +46,15 @@ class GithubCorner extends HTMLElement {
constructor() {
super()

this.#styleSheet = new CSSStyleSheet()
this.#styleSheet.replaceSync(cssContent)

// Create a shadow root
this.#shadowRoot = this.attachShadow({ mode: 'open' })

const style = document.createElement('style')
style.textContent = cssContent
this.#shadowRoot.adoptedStyleSheets = [this.#styleSheet]

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

this.#svgContainer = this.#shadowRoot.querySelector(
Expand Down Expand Up @@ -91,9 +92,6 @@ class GithubCorner extends HTMLElement {
}

#init() {
const size = this.getAttribute('size')
this.#setSize(size ? size : DEFAULT_SIZE)

if (!this.#banner.getAttribute('fill')) {
this.#banner.setAttribute('fill', DEFAULT_BANNER_COLOR)
}
Expand All @@ -110,11 +108,6 @@ class GithubCorner extends HTMLElement {
}
}

#setSize(size: string) {
this.style.width = size
this.style.height = size
}

connectedCallback() {
if (this.#status === 'init') {
this.#init()
Expand All @@ -137,7 +130,11 @@ class GithubCorner extends HTMLElement {
) {
switch (attributeName) {
case 'size':
this.#setSize(newValue)
// replace size custom property
this.#styleSheet.deleteRule(0)
this.#styleSheet.insertRule(
`:host { --github-corner-size: ${newValue}; }`
)
break

case 'href':
Expand Down
6 changes: 6 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
:host {
--github-corner-size: 5rem;
}

:host {
display: block;
position: absolute;
cursor: pointer;
overflow: hidden;
clip-path: polygon(0% 0%, 100% 0%, 100% 100%);
width: var(--github-corner-size);
height: var(--github-corner-size);
}

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