Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix binding style attribute with CSS variables #2581

Merged
merged 2 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/alpinejs/src/utils/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ function setStylesFromObject(el, value) {

// When we use javascript object, css properties use the camelCase
// syntax but when we use setProperty, we need the css format
// so we need to convert camelCase to kebab-case
el.style.setProperty(kebabCase(key), value)
// so we need to convert camelCase to kebab-case.
// In case key is a CSS variable, leave it as it is.
if (! key.startsWith('--')) {
key = kebabCase(key);
}

el.style.setProperty(key, value)
})

setTimeout(() => {
Expand Down
11 changes: 11 additions & 0 deletions tests/cypress/integration/directives/x-bind-style.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ test('style attribute object binding using kebab-case syntax',
}
)

test('style attribute object binding with CSS variable',
html`
<div x-data x-bind:style="{ '--MyCSS-Variable': 0.25 }">
<span style="opacity: var(--MyCSS-Variable);">I should be hardly visible</span>
</div>
`,
({ get }) => {
get('div').should(haveAttribute('style', '--MyCSS-Variable:0.25;'))
}
)

test('style attribute object bindings are merged with existing styles',
html`
<div x-data>
Expand Down