styled-theming icon indicating copy to clipboard operation
styled-theming copied to clipboard

Add support for boolean values for theme.variants

Open likethemammal opened this issue 4 years ago • 1 comments

Because the docs dont explicitly say otherwise, I assumed that boolean values would work as keys in the values object passed to theme.variants(). After some debugging and looking through the source I realized this wasnt possible because of a conditional that checked if the value's key was truthy.

These changes explicitly check if the value's key is undefined, which allows props passed to the styled Component to be boolean values.

Example usage:

theme.variants('someMode', 'hasSomeProp', {
  [true]: {
    light: 'some value',
    dark: 'some darker value',
  },
  [false]: {
    light: 'some other value',
    dark: 'some other darker value',
  }
})

likethemammal avatar Dec 27 '19 07:12 likethemammal

I came here looking for the same thing. I don't think that would work because an object's keys, by definition, can only be a string, number, or symbol. So I feel like this is a side-effect of checking for values using key names.

Instead, IMO this should allow a custom function, like:

theme.variants('someMode', 'hasSomeProp', val => {
  if (val === true) {
    return {
      light: 'some value',
      dark: 'some darker value',
    }
  }
  return {
    light: 'some other value',
    dark: 'some other darker value',
  }
})

matthew-dean avatar May 09 '22 15:05 matthew-dean