diff --git a/packages/@react-facet/dom-fiber/src/setupAttributes.ts b/packages/@react-facet/dom-fiber/src/setupAttributes.ts
index 62e29477..d8dbff77 100644
--- a/packages/@react-facet/dom-fiber/src/setupAttributes.ts
+++ b/packages/@react-facet/dom-fiber/src/setupAttributes.ts
@@ -10,15 +10,33 @@ export const setupStyleUpdate = (
const value = styleProp[key]
if (value !== undefined) {
+ // CSS custom properties (variables) require setProperty
+ const isCSSVariable = key[0] === '-'
+
if (isFacet(value)) {
- styleUnsubscribers.set(
- key,
- value.observe((value) => {
- style[key] = value
- }),
- )
+ if (isCSSVariable) {
+ const cssStyle = style as unknown as CSSStyleDeclaration
+ styleUnsubscribers.set(
+ key,
+ value.observe((value) => {
+ cssStyle.setProperty(key, value as string)
+ }),
+ )
+ } else {
+ styleUnsubscribers.set(
+ key,
+ value.observe((value) => {
+ style[key] = value
+ }),
+ )
+ }
} else {
- style[key] = value
+ if (isCSSVariable) {
+ const cssStyle = style as unknown as CSSStyleDeclaration
+ cssStyle.setProperty(key, value as string)
+ } else {
+ style[key] = value
+ }
}
}
}
diff --git a/packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx b/packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx
index ce5a7187..8f1d272b 100644
--- a/packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx
+++ b/packages/@react-facet/dom-fiber/src/setupHostConfig.spec.tsx
@@ -29,6 +29,12 @@ describe('mount', () => {
expect(root).toContainHTML('
')
})
+ it('sets CSS custom properties (variables)', () => {
+ render(Hello World
)
+ const div = root?.querySelector('div')
+ expect(div?.style.getPropertyValue('--my-color')).toBe('blue')
+ })
+
it('sets the src', () => {
render(
)
const img = document.getElementsByClassName('image')[0] as HTMLImageElement | undefined
@@ -307,6 +313,17 @@ describe('mount', () => {
expect(root).toContainHTML('')
})
+ it('sets CSS custom properties (variables) with facets', () => {
+ const colorFacet = createFacet({ initialValue: 'blue' })
+
+ render(Hello World)
+ const div = root?.querySelector('div')
+ expect(div?.style.getPropertyValue('--my-color')).toBe('blue')
+
+ colorFacet.set('green')
+ expect(div?.style.getPropertyValue('--my-color')).toBe('green')
+ })
+
it('sets the src', () => {
const srcFacet = createFacet({ initialValue: 'bananafrita.png' })
diff --git a/packages/@react-facet/dom-fiber/src/setupHostConfig.ts b/packages/@react-facet/dom-fiber/src/setupHostConfig.ts
index 32e59f10..3bce9f2b 100644
--- a/packages/@react-facet/dom-fiber/src/setupHostConfig.ts
+++ b/packages/@react-facet/dom-fiber/src/setupHostConfig.ts
@@ -390,15 +390,31 @@ export const setupHostConfig = (): HostConfig<
const newValue = newStyleProp[key]
if (oldValue !== newValue || oldStyleProp === undefined) {
+ // CSS custom properties (variables) require setProperty
+ const isCSSVariable = key[0] === '-'
+
if (isFacet(newValue)) {
- styleUnsubscribers.set(
- key,
- newValue.observe((value) => {
- notNullStyle[key] = value
- }),
- )
+ if (isCSSVariable) {
+ styleUnsubscribers.set(
+ key,
+ newValue.observe((value) => {
+ style.setProperty(key, value as string)
+ }),
+ )
+ } else {
+ styleUnsubscribers.set(
+ key,
+ newValue.observe((value) => {
+ notNullStyle[key] = value
+ }),
+ )
+ }
} else {
- notNullStyle[key] = newValue
+ if (isCSSVariable) {
+ style.setProperty(key, newValue as string)
+ } else {
+ notNullStyle[key] = newValue
+ }
}
}
}