Skip to content

Commit df9ccb1

Browse files
committed
refactor: add SSR support
1 parent dd7cdee commit df9ccb1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/getColor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import getStyle from './getStyle'
99

10-
const getColor = (rawProperty: string, element = document.body) => {
10+
const getColor = (rawProperty: string, element?: Element) => {
1111
const property = `--${rawProperty}`
1212
const style = getStyle(property, element)
1313
return style ? style : rawProperty

src/getStyle.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@
55
* --------------------------------------------------------------------------
66
*/
77

8-
const getStyle = (property: string, element = document.body) => {
9-
return window.getComputedStyle(element, null).getPropertyValue(property).replace(/^\s/, '')
8+
const getStyle = (property: string, element?: Element) => {
9+
if (typeof window === 'undefined') {
10+
return
11+
}
12+
13+
if (typeof document === 'undefined') {
14+
return
15+
}
16+
17+
const _element = element ?? document.body
18+
19+
return window.getComputedStyle(_element, null).getPropertyValue(property).replace(/^\s/, '')
1020
}
1121

1222
export default getStyle

0 commit comments

Comments
 (0)