Skip to content

Commit 3d24712

Browse files
fix: recognize className prop to AnimatedComponents (#2292)
* Recognize className attribute to AnimatedComponents * add changeset * fix typo in changeset * fix changeset typo v2 * test: add test for className prop handling * chore: run prettier --------- Co-authored-by: Josh <[email protected]>
1 parent fd65b60 commit 3d24712

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

.changeset/animated-className.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-spring/web': patch
3+
---
4+
5+
fix: recognize `className` prop to `AnimatedComponent`s

targets/web/src/animated.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ describe('animated component', () => {
106106
mockRaf.step()
107107
expect(wrapper.scrollTop).toBe(20)
108108
})
109+
it('accepts the className property', () => {
110+
const className = spring('test')
111+
const { getByTestId } = render(
112+
<a.div className={className} data-testid="wrapper" />
113+
)
114+
expect(getByTestId('wrapper').className).toBe('test')
115+
className.set('new')
116+
mockRaf.step()
117+
expect(getByTestId('wrapper').className).toBe('new')
118+
})
109119
it('accepts x/y/z as style keys equivalent to `translate3d`transform function', () => {
110120
const { queryByTestId, rerender } = render(
111121
<a.div style={{ x: 10 }} data-testid="wrapper" />

targets/web/src/applyAnimatedValues.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {
3030
instance.nodeName === 'filter' ||
3131
(instance.parentNode && instance.parentNode.nodeName === 'filter')
3232

33-
const { style, children, scrollTop, scrollLeft, viewBox, ...attributes } =
34-
props!
33+
const {
34+
className,
35+
style,
36+
children,
37+
scrollTop,
38+
scrollLeft,
39+
viewBox,
40+
...attributes
41+
} = props!
3542

3643
const values = Object.values(attributes)
3744
const names = Object.keys(attributes).map(name =>
@@ -66,6 +73,9 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {
6673
instance.setAttribute(name, values[i])
6774
})
6875

76+
if (className !== void 0) {
77+
instance.className = className
78+
}
6979
if (scrollTop !== void 0) {
7080
instance.scrollTop = scrollTop
7181
}

0 commit comments

Comments
 (0)