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

Checkbox slider with lose animation if label changes #1881

Closed
4ndv opened this issue Jul 20, 2017 · 3 comments
Closed

Checkbox slider with lose animation if label changes #1881

4ndv opened this issue Jul 20, 2017 · 3 comments

Comments

@4ndv
Copy link

4ndv commented Jul 20, 2017

Steps

const checkboxLabel = this.context.intl.formatMessage({ id: this.state.enabled ? 'autoreply.statusEnabled' : 'autoreply.statusDisabled' })

return (<Checkbox
            name='enabled'
            onChange={this.handleCheckboxChange.bind(this)}
            checked={this.state.enabled}
            slider
            label={checkboxLabel} />)

Expected Result

Animated Checkbox slider

Actual Result

No animation

Version

0.71.1

Testcase

https://codepen.io/andv/pen/YQmWJp?editors=0010

@levithomason
Copy link
Member

The Checkbox uses ::before and ::after pseudo elements to create the slider track and handle. When changing the label, React creates a whole new DOM node. This new node has its own ::before and ::after pseudo elements. This is why they are no longer animated, there is no "starting" point for the new psuedo elements. The are created at the "on" position immediately.

I'm not sure this is fixable without some ugly hacks or a whole lot of work. We would have to get around the basis of React, replacing nodes when props/state change.

A Workaround

Not ideal, but one workaround is to update the label after the animation is complete:

https://codepen.io/levithomason/pen/JyPRXG


I'll leave this open for a bit and see if anyone has any smarter ideas than myself. PRs and engineering very much appreciated.

@overthemike
Copy link

overthemike commented Sep 16, 2017

Brand spankin' new here so please forgive my ignorance and lack of understanding of the codebase...that being said however...

What about simply adding a span around the text of the label? This way the pseudo elements will still be attached to the label element itself while the span element itself is what is changing.

I attempted to look into testing this out, however I'm going to definitely need to familiarize myself before giving that a shot. Seems to be created using factories, so this will affect other elements as well.

@layershifter
Copy link
Member

I will close this for housekeeping. Issue really exists, however it a logical backgroud and I'm not sure that we responsive for its fix.

The easies way is use an wrapper element as @overthemike suggested:

<Checkbox
  checked={enabled}
  label={<label>{label}</label>}
  slider
/>

Working codepen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants