Skip to content

Commit

Permalink
allow for cssom and dom to accept a selector string to find the s…
Browse files Browse the repository at this point in the history
…erver rendered stylesheet
  • Loading branch information
sastan committed Nov 3, 2022
1 parent d481948 commit e2c17a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-ways-divide.md
@@ -0,0 +1,5 @@
---
'twind': patch
---

allow for `cssom` and `dom` to accept a selector string to find the server rendered stylesheet
17 changes: 11 additions & 6 deletions packages/twind/src/sheets.ts
Expand Up @@ -2,8 +2,8 @@ import { warn } from './internal/warn'
import type { Sheet, SheetRule } from './types'
import { asArray, noop } from './utils'

function getStyleElement(element?: Element | null | false): HTMLStyleElement {
let style = element || document.querySelector('style[data-twind]')
function getStyleElement(selector: string | null | undefined | false): HTMLStyleElement {
let style = document.querySelector(selector || 'style[data-twind]')

if (!style || style.tagName != 'STYLE') {
style = document.createElement('style')
Expand All @@ -14,10 +14,15 @@ function getStyleElement(element?: Element | null | false): HTMLStyleElement {
return style as HTMLStyleElement
}

export function cssom(element?: CSSStyleSheet | Element | null | false): Sheet<CSSStyleSheet> {
export function cssom(
element?: CSSStyleSheet | HTMLStyleElement | string | null | false,
): Sheet<CSSStyleSheet> {
const target = (element as CSSStyleSheet)?.cssRules
? (element as CSSStyleSheet)
: (getStyleElement(element as Element | null | false).sheet as CSSStyleSheet)
: ((element && typeof element != 'string'
? (element as HTMLStyleElement)
: getStyleElement(element)
).sheet as CSSStyleSheet)

return {
target,
Expand Down Expand Up @@ -69,8 +74,8 @@ export function cssom(element?: CSSStyleSheet | Element | null | false): Sheet<C
}
}

export function dom(element?: Element | null | false): Sheet<HTMLStyleElement> {
const target = getStyleElement(element)
export function dom(element?: HTMLStyleElement | string | null | false): Sheet<HTMLStyleElement> {
const target = element && typeof element != 'string' ? element : getStyleElement(element)

return {
target,
Expand Down

0 comments on commit e2c17a2

Please sign in to comment.