Code Monkey home page Code Monkey logo

Comments (11)

DevanB avatar DevanB commented on May 14, 2024 1

As a followup, I was able to turn:

const background = switchProp('color', {
  green: theme('green'),
  ink: theme('ink'),
  red: theme('red'),
  white: theme('white'),
});

background: ${background};

into:

background: ${withProp('color', incomingColor => theme(incomingColor))};

from styled-tools.

diegohaz avatar diegohaz commented on May 14, 2024 1

In that case, I guess this works:

withProp('color', theme)

from styled-tools.

diegohaz avatar diegohaz commented on May 14, 2024

Hey @DevanB

Try this:

background: ${withProp(theme("green", "red"), darken(0.1))};

More info: https://github.com/diegohaz/styled-tools#withprop

from styled-tools.

DevanB avatar DevanB commented on May 14, 2024

This seems to work!

So withProp(theme('green')) is replacing ${props => something(props.theme.green)}?

Also, we are evaluating your tools (which look very promising!) for use throughout our component library. Does something like this look like a good refactor? Or am I missing some great other usages of your library? 😄

image

from styled-tools.

diegohaz avatar diegohaz commented on May 14, 2024

So withProp(theme('green')) is replacing ${props => something(props.theme.green)}?

Exactly! 😉

I'd write this way:

const background = switchProp("color", {
  green: withProp(theme("green"), darken(0.1)),
  ink: withProp(theme("ink"), darken(0.15))
});

console.log(background({ color: "green", theme: { green: "green" } }));

const Comp = styled.div`
  background: ${background};
`;

Or, if the only variable thing were the prop value:

const background = withProp("color", color => withProp(theme(color), darken(0.1)));

This can be composed in different ways. Just be careful not to abstract to the point of reducing its legibility (splitting large compositions into well named functions helps).

Basically, all the functions return another function that receive props, and you can recursively call them.

from styled-tools.

diegohaz avatar diegohaz commented on May 14, 2024

Ah, and note that we're leveraging the curried version of polished function (you can use darken(arg1)(arg2))

from styled-tools.

DevanB avatar DevanB commented on May 14, 2024

@diegohaz Thanks much!! This is super valuable!

from styled-tools.

DevanB avatar DevanB commented on May 14, 2024

Fantastic! I'm going to make it a point to submit a PR for your documentation. For people like me that stumble upon this hidden gem, it might be good to have a little "recipes" section that shows basic and highly usable examples!

from styled-tools.

diegohaz avatar diegohaz commented on May 14, 2024

That would be really useful. Thank you! :)

from styled-tools.

DevanB avatar DevanB commented on May 14, 2024

@diegohaz one last question! Do you see any reason why this:

const hoverFocusBackground = switchProp('color', {
  green: withProp(theme('green'), darken(0.1)),
  ink: withProp(theme('ink'), lighten(0.15)),
  red: withProp(theme('red'), darken(0.1)),
  white: withProp(theme('white'), darken(0.1)),
});

would still throw an error of:

<Button /> › matches snapshot

Passed an incorrect argument to a color function, please pass a string representation of a color.

       8 | describe('<Button />', async () => {
       9 |   it('matches snapshot', () => {
    > 10 |     const { container } = render(<Button color="red">Test</Button>);
         |                           ^
      11 |     expect(container.firstChild).toMatchSnapshot();
      12 |   });
      13 | });

      at parseToRgb (node_modules/polished/lib/color/parseToRgb.js:42:11)
      at parseToHsl (node_modules/polished/lib/color/parseToHsl.js:34:57)
      at darken (node_modules/polished/lib/color/darken.js:58:42)

in my Jest snapshot tests?

The test is a basic snapshot test:

import React from 'react';
import { render } from 'react-testing-library';
import { Button } from '../Buttons';

describe('<Button />', async () => {
  it('matches snapshot', () => {
    const { container } = render(<Button color="red">Test</Button>);
    expect(container.firstChild).toMatchSnapshot();
  });
});

I wouldn't think something like this is affecting me: https://stackoverflow.com/questions/47704601/styled-components-polished-and-styledprops-darken-throwing-error

The best I can tell, you are returning a string when currying.

I created a CodeSandbox showing the issue here: https://codesandbox.io/s/18834qr52l

from styled-tools.

DevanB avatar DevanB commented on May 14, 2024

I'm so sorry for bothering. Figured out the problem was my tests didn't have access to theme because I wasn't 1) passing it in, nor 2) wrapping the test in ThemeProvider.

Thanks for dealing with me 😄

from styled-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.