Skip to content
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

v3 #40

Merged
merged 15 commits into from
Dec 7, 2021
Merged

v3 #40

merged 15 commits into from
Dec 7, 2021

Conversation

garronej
Copy link
Owner

The remaining question is: How to we make this work with withStyles.

@garronej
Copy link
Owner Author

garronej commented Nov 24, 2021

Hi @olee, what do you think about an API like the following one where we provide the refs as type only instead of passing an actual argument?

const useStyles = makeStyles<{ color: "red"; }, "child" | "child2">()((theme, { color }, refs) => ({
        "parent": {
            "padding": theme.spacing(3),
            [`&:hover .${refs.child}`]: {
                "background": color,
            },
            [`&:hover .${refs.child2}`]: {
                "background": "red",
            },
        },
        "child": {
            "background": "blue",
        },
        "child2": {
       }
}));

This could potentially unlock the situation we have with withStyles making the refs being inferable from which @mui is passed.

It's hard to achieve but using es5 Proxies we could theoretically pull it off. Proxy can be polifyied for IE.

@olee
Copy link

olee commented Nov 25, 2021

Why do you think this won't work with withStyles?
You can just use it the same way like this:

const MyComponent = withStyles((theme, props, refs) => ({
    parent: {
        padding: theme.spacing(3),
        [`&:hover .${refs.child}`]: {
            background: props.color,
        },
        [`&:hover .${refs.child2}`]: {
            background: "red",
        },
    },
    child: {
        background: "blue",
    },
    child2: {
    }
}), { name: 'MyComponent', refs: ['child', 'child2'] })(MyComponent);

If you check material-ui v4 withStyles you will actually see that it also has that options object as a second parameter:
image

@garronej
Copy link
Owner Author

Yes it could work but the difference between makeStyles and withStyles is that in withStyles the rules names are defined by the component passed as argument.
Take the mui <Button> for examples the rules names are "root" | "text" |"textInherit"...
The refs array element type would have to be a subset of theses rules.

It would be much cleaner if we could make it work without requiring the user to specify an information we already have...

@garronej
Copy link
Owner Author

Hi @olee,
I implemented, tested and documented the new API. I think it's ready for v3 🥳
I'll wait some days before releasing to see if you think there is things that needs to be changed.
Sorry I havent got the time to implement en beta release mechanism yet.
You can read at the top of the readme the breaking changes.

Thank you for your help, Best

@garronej garronej changed the title Implement new API based on refMap (not working with withStyles yet #35) Implement new API based on refMap #35 Nov 30, 2021
@olee
Copy link

olee commented Dec 1, 2021

Yeah I actually got my workplace to give me time to take care of this PR as we will be using this productively.
So if you can wait for this a little bit then I will be able to look into this properly at least until the end of next week 👍

@garronej garronej changed the title Implement new API based on refMap #35 v3 Dec 1, 2021
@garronej
Copy link
Owner Author

garronej commented Dec 2, 2021

I updated our CI framework to be able the publish beta versions.
There is now a beta version of tss v3 available on NPM.
I put it in production with our stuffs. I think it's ready for release.
I will however leave you some time to test and review before merging 👍🏻

@olee
Copy link

olee commented Dec 7, 2021

The counter should still be global imho, as creating multiple makeStyles when used by libraries etc. can still create duplicates. I think there is no issue in just moving up those few lines to be outside of the makeStyles function.

@garronej garronej merged commit 43bed21 into main Dec 7, 2021
@garronej garronej deleted the new_api_for_nested_selector branch December 7, 2021 17:04
@garronej
Copy link
Owner Author

garronej commented Dec 7, 2021

@olee alright! Thanks for the review. It's live.

@olee
Copy link

olee commented Dec 13, 2021

What about the other review comments? 😅

@garronej
Copy link
Owner Author

garronej commented Dec 13, 2021

@olee Sorry have I missed something?
I looked everywhere but and found nothing.
I maybe erased some stuff with a rebase?

@olee
Copy link

olee commented Dec 13, 2021

There's a bunch of review comments I created:
image

They are just above here in the feed 😕

@garronej
Copy link
Owner Author

Your review is pending, I can't see it yet 🙂
image

garronej added a commit that referenced this pull request Dec 13, 2021
@garronej
Copy link
Owner Author

garronej commented Dec 13, 2021

image
image
Thanks, good catch!!

image

I'm always using " for property when declaring an object. It help quickly see if we are declaring a type or creating an object. It improves a lot TypeScript code readability. 👍

I published a new release.

README.md Show resolved Hide resolved
src/makeStyles.tsx Show resolved Hide resolved
src/makeStyles.tsx Show resolved Hide resolved
src/makeStyles.tsx Show resolved Hide resolved
src/makeStyles.tsx Show resolved Hide resolved
src/makeStyles.tsx Show resolved Hide resolved

return useMemo(() => {
const refClassesCache: Record<string, string> = {};

const refClasses = new Proxy<
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if using a proxy is super smart or actually a bad idea here...
While on the one side it allows not having the define the ref names prior to the styles, it makes it necessary to provide explicit generic arguments to the makeStyles invocation which is quite annoying and it also provides no safety at all for non-typescript use (totally optional imho as pretty much everyone should use TS, but it's still a thing to consider).

Copy link
Owner Author

@garronej garronej Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicit generic arguments to the makeStyles invocation which is quite annoying

Passing { "ref": [ "a", "b", "c" ] } if anything is more verbose that "a" | "b" | "c". The only thing that pain me is to have to explicitly provide void as the Param argument. makeStyle<void, "a" | "b" | "c"> 😔

it also provides no safety at all for non-typescript use

Making explicitly provide the "ref": [ "a", "b", "c" ] provide no safety to the JS users. If anything it's one more place where they can screw up. In JSS they where used to $ruleName without having to provide an extra parameter. They expect the same with TSS.

There is also two extra argument in favor of using type only param instead of runtime param:

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well as mentioned before already, withStyles also has a third argument to pass options so that should not be an issue

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I provided the wrong gif.
I already explained it to you. Why would you require user to explicitly pass in an information we don't need and we already have?
It would be a fundamentally flawed design.
In withStyles, it's not the user that get to decide the rules name. They are defined by the type of theclasses props.

@olee
Copy link

olee commented Dec 15, 2021

Oh man... I didn't know I had to specifically "submit" a review because I never did this 😣

garronej added a commit that referenced this pull request Dec 15, 2021
@garronej
Copy link
Owner Author

Thank you very much for taking the time to review the code so thoughtfully.
Even if I don't agree with you on all the coding style aspect, your contribution is very much valuated.
Without you several important bugs would have remained unnoticed, we wouldn't have labeling and there would still be stuck with the createRef() API.

Many thanks.

garronej added a commit that referenced this pull request Dec 19, 2021
gitbook-com bot pushed a commit that referenced this pull request Jan 30, 2022
miltondaleandrade added a commit to miltondaleandrade/tss-react that referenced this pull request Feb 24, 2022
gitbook-com bot pushed a commit that referenced this pull request Oct 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants