-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(front): merge state to
useDynStyle
- Loading branch information
1 parent
4d310e9
commit d0aeca3
Showing
3 changed files
with
13 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import { useInsertionEffect } from "react"; | ||
import { useStorageState } from "@/hooks"; | ||
|
||
/** | ||
* Inject CSS dynamically on the client side. | ||
* # NOTE | ||
* Frequent style recalculation is inevitable, | ||
* but this hook can solve the delay problem caused by style injection lifecycle discrepancies. | ||
* - See: [useInsertionEffect](https://react.dev/reference/react/useInsertionEffect) | ||
* @param css | ||
*/ | ||
export function useDynStyle(css: string) { | ||
export function useDynStyle() { | ||
const [style, setStyle] = useStorageState("customCSS"); | ||
|
||
useInsertionEffect(() => { | ||
const styleElement = document.createElement("style"); | ||
styleElement.innerHTML = css; | ||
styleElement.innerHTML = style; | ||
document.head.appendChild(styleElement); | ||
return () => { | ||
document.head.removeChild(styleElement); | ||
}; | ||
}, [css]); | ||
}, [style]); | ||
|
||
return [style, setStyle] as const; | ||
} |