Home > Software design >  Restyle createRestyleFunction
Restyle createRestyleFunction

Time:05-15

Can anyone help with me understanding the purpose of this function?

Does anyone have a real life example of a time they implemented it?

I understand the example given but I cant see a real world need to rename opacity to transparency so I am sure there are some real problems that it can solve?

CodePudding user response:

The example given in the docs does more than remap the name of opacity - it inverts the value. The example could be more interesting, as transform also takes a theme and themeKey. I made a more involved example that uses these. So, for a value of progress from 0-1, you get one color for your simple theme, a shade of yellow for your dark theme, and a shade of blue for your light theme.

const transparency = createRestyleFunction({
  property: 'progress',
  styleProperty: 'color',
  transform: ({value, theme, themeKey}) => themeKey === 'simple'
    ? theme['color']
    : `rgb(${
        themeKey === 'dark' ? 250 * value : 0
      }, ${
        themeKey === 'dark' ? 200 * value : 0
      }, ${
        themeKey === 'dark' ? 0 : value * 250
      })`,
});

So it's basically a more powerful tool to create aliases/abbreviations (restyled is riddled with these), which also allows you to transform the value, potentially based on your chosen theme. I doubt it's widely used but it is potentially interesting.

  • Related