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

Fix for #650: Generate friendly class names #916

Conversation

LucasUnplugged
Copy link
Contributor

(Addresses #650)

I'm giving this one another try, with the latest version (plus my suggested fixes from PRs #893 and #900).

I've been working from a build off this branch for a couple of weeks now, with no adverse side-effects. This has resulted in this kind of rendered HTML:

<section class="c-Box-lesPJm c-Flex-dhzjXW c-Column-iTKOFX c-Box-lesPJm-cScfiu-mb-6">
...
</section>

...which at this point, my brain quickly reads as (by ignoring the hashes):

<section class="c-Box c-Flex c-Column c-Box-variant-mb-6">
...
</section>

I find that vastly more helpful, for debugging purposes, than a sea of hashed classes that mean nothing to me.


Proposed API

Users can pass in a string as the last parameter of their calls to styled, like so:

// Renders as `c-Label-sOm3h4Sh`
const Label = styled("label", {...}, "Label")

This proposed solution can be combined with a babel plugin, to make friendly class names possible with no extra effort, for those using Babel.


Related Discussion

There was a related discussion at #677, but the solution proposed there by the Stitches team (defaultProps) was removed from the v1 API.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 3, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 72d7c8c:

Sandbox Source
Stitches CI: CRA Configuration
Stitches CI: Next.js Configuration

@LucasUnplugged
Copy link
Contributor Author

why is this being blocked?

I'm not sure it's being blocked, so much as the Stitches team has been focused elsewhere; to my knowledge, it is not an intentional blocking.

Copy link
Contributor

@luixo luixo left a comment

Choose a reason for hiding this comment

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

Should we add tests for the feature?
I can take over a unit test.

@LucasUnplugged
Copy link
Contributor Author

Should we add tests for the feature?

I can take over a unit test.

Oh, good point. I think I had done so already in my old commit; I'll see if I can find them and add them here. Thanks!

const createComposer = (/** @type {InitComposer} */ { variants: initSingularVariants, compoundVariants: initCompoundVariants, defaultVariants: initDefaultVariants, ...style }, /** @type {Config} */ config) => {
const createComposer = (/** @type {InitComposer} */ { variants: initSingularVariants, compoundVariants: initCompoundVariants, defaultVariants: initDefaultVariants, componentName, ...style }, /** @type {Config} */ config) => {
/** @type {string} */
const hash = componentName ? `${componentName}-${toHash(style)}` : toHash(style)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe componentName is not a part of the hash.
It probably should be something like

const className = toTailHash(config.prefix) + ['c', componentName, toHash(style)].filter(Boolean).join('-');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I understand you correctly (and I'm not sure I do), I think both pieces of code are doing the same thing; it's just my use of a variable name hash that is causing confusion.

My code is equivalent to doing this (note the change from hash to baseClass):

let baseClass = toHash(style)
if (componentName) {
  baseClass = `${componentName}-${baseClass}`
}
const className = `${toTailDashed(config.prefix)}c-${baseClass}`

So in both your code and mine, the componentName is getting conditionally added after the prefix and c-, but before the generated hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I'll change the name of the variable, to avoid confusion. Let me know if you're okay with that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, they're doing the same thing, my note was about naming.
Thanks!

@LucasUnplugged
Copy link
Contributor Author

I've added some tests to both core and react packages, and changed that confusing variable name.

@hadihallak hadihallak self-assigned this Feb 9, 2022
@guipolitano
Copy link

Hope this PR gets merged soon

@hadihallak
Copy link
Member

Hope this PR gets merged soon

@guipolitano Sadly, i don't think it's possible to get this merged as is as the API conflicts with another stitches API which is composing n number of styles into one component

const Button = styled('button', {...styles}, {...morestyles}, anotherStyledComponent)

To be clear, We love this feature and we know how important it is for the reasons outlined in the PR but we just need to think more about the API in order to maintain the DX that everyone loves about Stitches

We will think more about this and hopefully come back with a plan/decision for the API sometime next week and discuss it with @LucasUnplugged and everyone in here.

@LucasUnplugged
Copy link
Contributor Author

@guipolitano Sadly, i don't think it's possible to get this merged as is as the API conflicts with another stitches API which is composing n number of styles into one component


const Button = styled('button', {...styles}, {...morestyles}, anotherStyledComponent)

@hadihallak my code shouldn't conflict with that usage, because it only applies if there are at least 2 arguments, and the last argument is a string. In your example, only the first argument is a string, so it should still work as intended.

@hadihallak
Copy link
Member

That's true but it's re-using the same API which we've so far only reserved for styling so i'm wondering if we can do it differently so that it can be more clearly documented and easily readable for someone who isn't familiar with Stitches and reading the code for the first time

Maybe we could add a method or something similar that more explicitly tells the user what's going on behind the scenes.
We will think this through and get back and discuss things here with you

@LucasUnplugged
Copy link
Contributor Author

That makes sense, @hadihallak. Something like this would indeed be clearer:

const Button = styled('button', {...}).named('Button').

I'm not even sure if that's (realistically) possible, so didn't even attempt it.

@guipolitano
Copy link

Hope this PR gets merged soon

@guipolitano Sadly, i don't think it's possible to get this merged as is as the API conflicts with another stitches API which is composing n number of styles into one component

const Button = styled('button', {...styles}, {...morestyles}, anotherStyledComponent)

To be clear, We love this feature and we know how important it is for the reasons outlined in the PR but we just need to think more about the API in order to maintain the DX that everyone loves about Stitches

We will think more about this and hopefully come back with a plan/decision for the API sometime next week and discuss it with @LucasUnplugged and everyone in here.

Sadly =/.
Maybe it wouldn't be better to use babel-plugin, just as Styled-Components do? I had try to create one, but i know nothing about it so I couldn't achieve it.

@hadihallak
Copy link
Member

@guipolitano
yeah that's actually one of the main reason we need this feature. to enable us to automate the process of creating stable hashes and friendly classNames which is currently not possible as we have no API underneath to support it yet

@guipolitano
Copy link

@guipolitano yeah that's actually one of the main reason we need this feature. to enable us to automate the process of creating stable hashes and friendly classNames which is currently not possible as we have no API underneath to support it yet

Understood, hope you guys find a way. Thx for the great job, im loving to use Stitches with Radix. :)

@hadihallak
Copy link
Member

Closing in favor of #1005

@hadihallak hadihallak closed this Jun 6, 2022
@bartcheers
Copy link

Closing the loop here: withConfig has been introduced after this issue was closed, achieving the desired behavior. It's still missing in the docs, but instructions can be found here.

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.

5 participants