Skip to content

Commit

Permalink
Lazy initialize StyleSheet for better handling
Browse files Browse the repository at this point in the history
  • Loading branch information
drozdzynski committed May 29, 2024
1 parent 2416abd commit 561aaa4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/big-tomatoes-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@teiler/svelte": patch
"@teiler/vue": patch
---

Lazy initialize StyleSheet for better handling
14 changes: 12 additions & 2 deletions packages/svelte/src/sheet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { type Sheet, createStyleSheet } from '@teiler/core'
import { getContext } from 'svelte'

export const styleSheet = createStyleSheet({})
export let styleSheet = null

export const StyleSheet = 'STYLE_SHEET'

export function getStyleSheet(): Sheet {
return getContext(StyleSheet) || styleSheet
const fromContext = getContext<Sheet>(StyleSheet)

if (fromContext) {
return fromContext
}

if (styleSheet === null) {
styleSheet = createStyleSheet({})
}

return styleSheet
}
5 changes: 3 additions & 2 deletions packages/vue/src/Styled.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { HTMLElements, Sheet, StyleDefinition } from '@teiler/core'

import { createStyleSheet, insert } from '@teiler/core'
import { insert } from '@teiler/core'
import { defineComponent, h, inject, toRaw, toValue } from 'vue'
import { context } from './ThemeProvider'
import { getStyleSheet } from './sheet'

export default function <Target extends HTMLElements, Props>(styleDefinition: StyleDefinition<Target, Props>) {
const component = defineComponent({
Expand All @@ -12,7 +13,7 @@ export default function <Target extends HTMLElements, Props>(styleDefinition: St
const slots = this.$slots
const attrs = toRaw(this.$attrs)

const styleSheet = inject<Sheet>('STYLE_SHEET', () => createStyleSheet({}), true)
const styleSheet: Sheet = getStyleSheet()

const theme = toRaw(toValue(inject(context, {})))

Expand Down
20 changes: 20 additions & 0 deletions packages/vue/src/sheet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { type Sheet, createStyleSheet } from '@teiler/core'
import { inject } from 'vue'

export let styleSheet = null

export const StyleSheet = 'STYLE_SHEET'

export function getStyleSheet(): Sheet {
const provided = inject<Sheet>('STYLE_SHEET', () => createStyleSheet({}), true)

if (provided) {
return provided
}

if (styleSheet === null) {
styleSheet = createStyleSheet({})
}

return styleSheet
}

0 comments on commit 561aaa4

Please sign in to comment.