Code Monkey home page Code Monkey logo

Comments (9)

jtmthf avatar jtmthf commented on May 14, 2024 1

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.

diegohaz avatar diegohaz commented on May 14, 2024 1

I think deepProp, deepTheme and deepPalette are enough. People could do withProp(deepProp(...)) for the others.

from styled-tools.

jtmthf avatar jtmthf commented on May 14, 2024 1

Okay I can create a PR for that.

from styled-tools.

diegohaz avatar diegohaz commented on May 14, 2024

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.

jtmthf avatar jtmthf commented on May 14, 2024

@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.

diegohaz avatar diegohaz commented on May 14, 2024

Reopening as we need to figure out a better solution (the other one introduced breaking changes #57).

from styled-tools.

jtmthf avatar jtmthf commented on May 14, 2024

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.

diegohaz avatar diegohaz commented on May 14, 2024

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.

jtmthf avatar jtmthf commented on May 14, 2024

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)

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.