-
Notifications
You must be signed in to change notification settings - Fork 31
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
theme usage with darken from polished #52
Comments
Hey @DevanB Try this:
More info: https://github.com/diegohaz/styled-tools#withprop |
This seems to work! So 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? 😄 |
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. |
Ah, and note that we're leveraging the curried version of polished function (you can use |
@diegohaz Thanks much!! This is super valuable! |
As a followup, I was able to turn:
into:
|
In that case, I guess this works: withProp('color', theme) |
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! |
That would be really useful. Thank you! :) |
@diegohaz one last question! Do you see any reason why this:
would still throw an error of:
in my Jest snapshot tests? The test is a basic snapshot test:
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 |
I'm so sorry for bothering. Figured out the problem was my tests didn't have access to Thanks for dealing with me 😄 |
Hey there!
Thanks for a great package full of lovely tools!
I'm running into an issue I don't quite understand:
causes a TypeScript error (in my TS codebase):
[ts] Argument of type '<Props, Theme extends { [key: string]: any; }>(props: Props & { theme: Theme; }) => string | Theme[keyof Theme]' is not assignable to parameter of type 'string'.
The best I can figure out is that
darken
s types (darken(amount: string | number, color: string): string
) expect a string, buttheme
isn't necessarily giving a string (it could be givingTheme[keyof Theme]
). Would you have any understanding of how to solve this problem?Thanks!
The text was updated successfully, but these errors were encountered: