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

support removing attributes from animated elements #2293

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/animate-attribute-removal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-spring/web': patch
---

fix: support removing attributes from animated elements
19 changes: 14 additions & 5 deletions targets/web/src/applyAnimatedValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const attributeCache: Lookup<string> = {}
type Instance = HTMLDivElement & { style?: Lookup }

export function applyAnimatedValues(instance: Instance, props: Lookup) {
if (!instance.nodeType || !instance.setAttribute) {
if (!instance.nodeType || !instance.setAttribute || !instance.removeAttribute) {
return false
}

Expand All @@ -45,7 +45,7 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {
))
)

if (children !== void 0) {
if (props.hasOwnProperty('children')) {
instance.textContent = children
}

Expand All @@ -63,7 +63,12 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {

// Apply DOM attributes
names.forEach((name, i) => {
instance.setAttribute(name, values[i])
const value = values[i]
if (value !== void 0) {
instance.setAttribute(name, value)
} else {
instance.removeAttribute(name)
}
})

if (scrollTop !== void 0) {
Expand All @@ -72,8 +77,12 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {
if (scrollLeft !== void 0) {
instance.scrollLeft = scrollLeft
}
if (viewBox !== void 0) {
instance.setAttribute('viewBox', viewBox)
if (props.hasOwnProperty('viewBox')) {
if (viewBox !== void 0) {
instance.setAttribute('viewBox', viewBox)
} else {
instance.removeAttribute('viewBox')
}
}
}

Expand Down
Loading