Comments (9)
I thought more about the "deep function values" test case as it may not be needed with styled-components. Assuming the result of ifProp
is interpolated into a style block, styled-components should deeply resolve the value itself. However other frameworks such as emotion don't resolve interpolated functions in css
blocks. As such I think it's best that styled-tools deeply resolves result values for better compatibility with those frameworks.
from styled-tools.
I think deepProp
, deepTheme
and deepPalette
are enough. People could do withProp(deepProp(...))
for the others.
from styled-tools.
Okay I can create a PR for that.
from styled-tools.
Hi @jtmthf. I'm not sure if I got it.
export const contrastText = (bgNeedle: Needle<any>) =>
withProp(
[bgNeedle, p('black'), p('white')] as any,
(bg: string, black: string, white: string) => {
// bg ends up being a prop getter function here instead of a string.
return getLuminance(bg) > 0.179 ? black : white;
},
);
Are black
and white
strings?
If you could create some failing tests, I think it would be easier to understand. Either way, I agree with your title withProp should recursively resolve prop getters
. So PR is really welcome. :)
from styled-tools.
@diegohaz already on a PR!
In order to be consistent, I think that any function that resolves a prop-getter should deeply resolve the value. As of now, I'm working on ifProp
. Here are just the tests that I've already added that were failing originally.
test("deep function argument", () => {
expect(ifProp(() => props => props.foo, "yes", "no")()).toBe("no");
expect(ifProp(() => props => props.foo, "yes", "no")({ foo: false })).toBe(
"no"
);
expect(ifProp(() => props => props.foo, "yes", "no")({ foo: true })).toBe(
"yes"
);
});
test("deep function values", () => {
expect(
ifProp("foo", () => props => props.foo, () => props => props.bar)({
bar: "bar"
})
).toBe("bar");
expect(
ifProp("foo", () => props => props.foo, () => props => props.bar)({
foo: "foo"
})
).toBe("foo");
});
test("deep function array argument", () => {
expect(
ifProp([() => props => props.foo], "yes", "no")({ bar: false, foo: true })
).toBe("yes");
expect(
ifProp(["bar", () => props => props.foo], "yes", () => "no")({
bar: false,
foo: true
})
).toBe("no");
});
from styled-tools.
Reopening as we need to figure out a better solution (the other one introduced breaking changes #57).
from styled-tools.
I think it makes sense that ifProp(prop: string, ...)
does not try try to resolve prop
deeply as ifProp
is only trying to determine the type of the prop and not to resolve its value. This could be fixed by parseObject
and parseString
not using the prop
function internally anymore. In order to preserve dot property lookup, the dot property functionality of prop
can be extracted out.
In the case that someone does want ifProp
to deeply resolve the prop, they can still use ifProp(prop('transparent'))
. For switchProp
and and withProp
, I do think those should deeply resolve their prop as the intent is to consume the value. The downsides of this being both inconsistency with ifProp
and technically being a breaking change even though it's hard to imagine a use case where you would not want the needle of switchProp
or withProp
deeply resolved.
from styled-tools.
What about a deep version of prop
?
withProp(deepProp(props => props => props.foo), x => x);
ifProp(deepProp(props => props => props.foo), true, false);
...
from styled-tools.
That might be more clear. As theme
and palette
only take keys, should a deepTheme
and deepPalette
be provided, or should the existing functions internally use deepProp
?
Additionally would it be worth providing withDeepProp
, switchDeepProp
, ifDeepProp
, and ifNotDeepProp
helpers or does that provide little benefit?
from styled-tools.
Related Issues (20)
- Confusing example HOT 1
- Add switchProp HOT 1
- Delete me!
- prop() not setting default HOT 2
- Styled-tools not working HOT 4
- Add default case option to switchProp HOT 4
- Named exports not detected when using Rollup HOT 1
- Add TypeScript type definitions HOT 2
- Drop support for array needle
- Typescript Support? HOT 2
- theme usage with darken from polished HOT 11
- TS error when using prop with ThemedStyledComponentsModule HOT 11
- Heads-up: 1.7.0 was a breaking change for us HOT 2
- How to get the {palette}Text color ? HOT 2
- Add comments to the typescript definitions [enhancement] HOT 1
- unable to interpolate ${prop('__')} HOT 1
- invalid valid for prop 'color' on <span /> tag HOT 1
- Is there any way to pass multiple props to an ifProp? HOT 3
- Improve Palette Types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from styled-tools.