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 (try 2) #2319

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 18 additions & 5 deletions targets/web/src/applyAnimatedValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ 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 Down Expand Up @@ -52,7 +56,7 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {
))
)

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

Expand All @@ -70,7 +74,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 (className !== void 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we apply the same logic of removing to all the props here? Just wondering the rationale behind the ones you've changed at the moment.

Expand All @@ -82,8 +91,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