Skip to content

Commit

Permalink
fix: recognize className prop to AnimatedComponents (#2292)
Browse files Browse the repository at this point in the history
* 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 <37798644+joshuaellis@users.noreply.github.com>
  • Loading branch information
LoganDark and joshuaellis committed Sep 16, 2024
1 parent fd65b60 commit 3d24712
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/animated-className.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@react-spring/web': patch
---

fix: recognize `className` prop to `AnimatedComponent`s
10 changes: 10 additions & 0 deletions targets/web/src/animated.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ describe('animated component', () => {
mockRaf.step()
expect(wrapper.scrollTop).toBe(20)
})
it('accepts the className property', () => {
const className = spring('test')
const { getByTestId } = render(
<a.div className={className} data-testid="wrapper" />
)
expect(getByTestId('wrapper').className).toBe('test')
className.set('new')
mockRaf.step()
expect(getByTestId('wrapper').className).toBe('new')
})
it('accepts x/y/z as style keys equivalent to `translate3d`transform function', () => {
const { queryByTestId, rerender } = render(
<a.div style={{ x: 10 }} data-testid="wrapper" />
Expand Down
14 changes: 12 additions & 2 deletions targets/web/src/applyAnimatedValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ export function applyAnimatedValues(instance: Instance, props: Lookup) {
instance.nodeName === 'filter' ||
(instance.parentNode && instance.parentNode.nodeName === 'filter')

const { style, children, scrollTop, scrollLeft, viewBox, ...attributes } =
props!
const {
className,
style,
children,
scrollTop,
scrollLeft,
viewBox,
...attributes
} = props!

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

if (className !== void 0) {
instance.className = className
}
if (scrollTop !== void 0) {
instance.scrollTop = scrollTop
}
Expand Down

0 comments on commit 3d24712

Please sign in to comment.