-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor #3965 - Add useStyle hook for dynamic styling
- Loading branch information
1 parent
3c46461
commit 9911c8f
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Ported from useStyleTag in @vueuse/core | ||
* https://github.com/vueuse | ||
*/ | ||
import { DomHandler } from 'primevue/utils'; | ||
import { getCurrentInstance, nextTick, onMounted, readonly, ref, watch } from 'vue'; | ||
|
||
function tryOnMounted(fn, sync = true) { | ||
if (getCurrentInstance()) onMounted(fn); | ||
else if (sync) fn(); | ||
else nextTick(fn); | ||
} | ||
|
||
let _id = 0; | ||
|
||
export function useStyle(css, options = {}) { | ||
const isLoaded = ref(false); | ||
|
||
const defaultDocument = DomHandler.isClient() ? window.document : undefined; | ||
const { document = defaultDocument, immediate = true, manual = false, id = `primevue_style_${++_id}` } = options; | ||
|
||
const cssRef = ref(css); | ||
|
||
let stop = () => {}; | ||
|
||
const load = () => { | ||
if (!document) return; | ||
|
||
const el = document.getElementById(id) || document.createElement('style'); | ||
|
||
if (!el.isConnected) { | ||
el.type = 'text/css'; | ||
el.id = id; | ||
if (options.media) el.media = options.media; | ||
document.head.appendChild(el); | ||
} | ||
|
||
if (isLoaded.value) return; | ||
|
||
stop = watch( | ||
cssRef, | ||
(value) => { | ||
el.textContent = value; | ||
}, | ||
{ immediate: true } | ||
); | ||
|
||
isLoaded.value = true; | ||
}; | ||
|
||
const unload = () => { | ||
if (!document || !isLoaded.value) return; | ||
stop(); | ||
document.head.removeChild(document.getElementById(id)); | ||
isLoaded.value = false; | ||
}; | ||
|
||
if (immediate && !manual) tryOnMounted(load); | ||
|
||
/*if (!manual) | ||
tryOnScopeDispose(unload)*/ | ||
|
||
return { | ||
id, | ||
css: cssRef, | ||
unload, | ||
load, | ||
isLoaded: readonly(isLoaded) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"main": "./usestyle.cjs.js", | ||
"module": "./usestyle.esm.js", | ||
"unpkg": "./usestyle.min.js", | ||
"types": "./UseStyle.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters