Giter Site home page Giter Site logo

Solution for composition? about tss-react HOT 6 CLOSED

garronej avatar garronej commented on July 20, 2024
Solution for composition?

from tss-react.

Comments (6)

awdyson avatar awdyson commented on July 20, 2024 1

Sorry, I should have been more clear.

I'm looking to share styling between classes, within the context of a single createUseClassNames call.
In the example above, I want a link's :hover state to look the same as its react router activeClassName state.
With JSS, there exists the extend plugin: https://cssinjs.org/jss-plugin-extend/
The best plan I've come up with for this would be an external function that's called with theme/props.

So, to rework the example above:

const helper = (theme, props) => ({
  // shared style that relies on theme and/or props
});

const { useClassNames } = createUseClassNames<Props>()((theme, props) => ({
  link: {
    // other styles
    '&:hover':  helper(theme, props),
  },
  activeLink: helper(theme, props),
}));

Hopefully this makes sense.
Also, this is definitely more of a nice to have, considering there's a workaround.
I figured it was in-line with your projects goals though.

from tss-react.

garronej avatar garronej commented on July 20, 2024 1

@awdyson This is actually a very good point, we need to support that.
Very easy to implement but hard to type.
I will try to think of something...

from tss-react.

awdyson avatar awdyson commented on July 20, 2024 1

Haven't been able to work on this project in a while, so I'm catching up on a lot of updates.

Your solution looks great -- and is the sort of thing I couldn't do well in JSS+MUI for a reason I can't remember.
Also, the method and property names look a lot cleaner these days.
Appreciate the great work 😃

from tss-react.

garronej avatar garronej commented on July 20, 2024

Sorry for late reply @awdyson ,
Sure you can it's a very important requirement!

type Props ={
    size: "big" | "small";
};

const { useClassNames } = createUseClassNames<Props & { color: "red" | "blue" }>()(
    (theme, { size, color }) => ({
        link: {
        ...
        '&:hover':  {
          extend: 'activeLink', // JSS solution
        },
      },
      activeLink: {
          // Here you have access to "size" from Props and "color" from internal state 
      },
}));


function MyComponent(props: Props){

    const [ color, setColor ] = useState<"red" | "blue">("red");

    const { classNames } = useClassNames({ ...props, color });


   //...

}

Best regard

from tss-react.

garronej avatar garronej commented on July 20, 2024

I will, soon, implement something like that:

const { classes, getLinkCss } = makeStyles<Props>()(
    (theme, props) => ({
        "link": {
            // other styles
            "&:hover":  {
               //style specific to hover link
           },
        },
        "activeLink": {
           ...getLinkCss(theme, props)["&:hover"],
           //style specific to activeLink
        }
    })
);

//getLinkCss can be exported for reuse outside of the file.
export { getLinkCss };

We'll get correct typing of getLinkCss, getActiveLinkCss using template literal types.

There will be some voodoo involved to prevent infinite recursion behind the seen but it will be transparent to the user.

from tss-react.

garronej avatar garronej commented on July 20, 2024

In the end I figured out that there is no need for a dedicated API.
Composition documentation

from tss-react.

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.