react-switch icon indicating copy to clipboard operation
react-switch copied to clipboard

Allow *Color to accept css variables, not only hex-colors

Open dmytro-lymarenko opened this issue 5 years ago • 5 comments

Allow offColor, onColor, offHandleColor and onHandleColor to accept css variables, not only hex-colors.

In our project we use css vars to handle themes and we use them to customize components. But react-switch component doesn't allow to do this.

I would like to use switch component like this:

<Switch
	checked={value}
	onChange={onChange}
	onColor="var(--switch-on-color)"
	offColor="var(--switch-off-color)"
/>

dmytro-lymarenko avatar Mar 16 '20 08:03 dmytro-lymarenko

Same here, I only use variable to customize my components color, I would really like this feature.

DiegoDevBittencourt avatar May 27 '20 01:05 DiegoDevBittencourt

UP! I would also like this feature.

MrWillian avatar Jul 18 '20 00:07 MrWillian

As long as your variable is a hex value, you can pull the value from JS.

        window
        .getComputedStyle(document.documentElement)
        .getPropertyValue('--switch-on-color')
        .trim()

sec0ndhand avatar Sep 30 '20 06:09 sec0ndhand

As @sec0ndhand mentioned you could extract the hex values from the CSS variables and then input them as props.

The question is whether this should be done inside the library for convenience sake and I'm leaning towards no. A couple of problems come to mind:

  • The library user might not have put their CSS variables on :root, in which case @sec0ndhand's snippet will fail.
  • The CSS variables might not be available when the component is first rendered which could be very difficult for the library user to debug.

markusenglund avatar Dec 12 '20 21:12 markusenglund