bento: add jss styling for amp-carousel.#29713
Conversation
| advance(by); | ||
| }; | ||
| const styles = useStyles(); | ||
| let classNames = |
There was a problem hiding this comment.
Nit, but this might be better:
const classNames = `${styles.arrowPlacement} ${by < 0 ? styles.arrowLeft : styles.arrowRight} ${isDisabled ? styles.arrowDisabled : ''}`;
Just looking for good patterns here since we'll be doing this quite a bit.
There was a problem hiding this comment.
Done.
I'm in favor of a classnames like helper lib going forward. Would transform this to the much clearer:
const classNames = cx(
styles.arrowPlacement,
by < 0 ? styles.arrowLeft : styles.arrowRight,
{[styles.arrowDisabled]: isDisabled}
)|
|
||
| // TODO discuss this sidestepping of local/no-export-side-effect. | ||
| // eslint-disable-next-line local/no-export-side-effect | ||
| export const useStyles = createUseStyles(styles); |
There was a problem hiding this comment.
I liked your idea of putting this into the base-carousel.jss.js because:
- It removes the danger of misusing the style objects.
- It makes it really obvious where to find
useStylesstyles. - Limits the
react-jssimport to only one type of module. - Simplifies rewriting conceptually - only one type of module is rewritten. It could also make rewriting slightly more difficult technically, but I think it's a good tradeoff.
- Also, down the line I'd like a lint that only allows
react-jssandjssto be imported in a.jss.jsmodules.
|
Hey @wassgha! These files were changed: |
dvoytenko
left a comment
There was a problem hiding this comment.
This is very close to mergeable. Just one more recommendation that I think would get it even closer and less disruptive.
| container./* OK */ scrollLeft += container./* OK */ offsetWidth * by; | ||
| }, | ||
| })); | ||
| // TODO: uncomment when build step is ready.. |
There was a problem hiding this comment.
Here's an idea. Let's actually create the useStyles() fake in the .jss.js file. It'd simply look like this:
export function useStyles() {
return classes;
}
Then we can do a couple of things:
- All component modules would look exactly like we want them to look. When build step is ready, none of them would need to change.
- We can shield
classesexport and thus reduce public surface of thejssfile.
There was a problem hiding this comment.
I wish I had thought of this. on it!
|
|
||
| // export const useStyles = createUseStyles(JSS); | ||
| // TODO: remove in favor of line above when build step is ready. | ||
| if (!globalThis.AMP) { |
There was a problem hiding this comment.
Per suggestion about exporting fake useStyles. We can use that same fake to exist this code. And as soon as the build step is complete, it all becomes unneeded.
| * | ||
| * @return {{classes: Object<string, string>}} | ||
| */ | ||
| export function useStyles() { |
There was a problem hiding this comment.
Much happier with this approach.
| const classes = useStyles(); | ||
| const classNames = `${classes.arrowPlacement} ${ | ||
| by < 0 ? classes.arrowPrev : classes.arrowNext | ||
| } ${isDisabled ? classes.arrowDisabled : ''}`; |
There was a problem hiding this comment.
Question: Do we prefer using classes.arrowDisabled for more precise specificity, or would we prefer defining a CSS rule for ${classes.arrowPlacement}[disabled]? With the second approach we would also append disabled={isDisabled} as the prop.
There was a problem hiding this comment.
Here's my quick 2 cents on this. The most purist CSS way is .x:disabled {} - but that's not always applicable. The other options are .x[disabled] {} and .x.x-disabled {}. The important considerations here are:
- The most important thing for us here is specificity. And all
.x:disabled,.x[disabled]and.x.x-disabledall have exactly the same specificity (https://specificity.keegan.st/). So no clear winners there. However, the special case we can make.x-disabled {}that would include combined styles. This will have a lower specificity. But, I think this is a rare case where a lower specificity is actually harmful. I think the combined selector is a good thing. .x[disabled]IMHO "feels" more semantic and self-documenting. But this is potentially less visible inside a component..x.x-disabledis faster, though in this case shouldn't be too big of a difference..x.x-disabledis slightly repetitive. We still need to add thedisabledandaria-disabledattributes.
I think the bigger answer is that there's no a clear winner here. We can keep moving with a class and reassess/change later since these are fully compatible options due to the same specificity.
* bento: add jss styling * naive approach, litter with TODOs * move jss to deps vs. devDep * updates after discussion with Dima * hover/active styles * forgot a file, some fixes * it builds, but useStyles throws * className --> class * works * switch back to useStyles throughout now that hooks are unbroken with the alias * use eslint disable * nit * make mergable * final dima suggestions * fix css * forgot an override * no idea how className crept back in * fix * fix type * FIX Types
summary
Implements the preact/storybook half of the Bento: stylesheets via JSS proposal
jss,react-jss)react/react-domto the preact equivalent, which enablesreact-jssto use the preact hooks..css.jsand inline styles into equivalent jss.:hover/:activeto css.Note: I think it would be nice to either import or create our own version of the classnames lib to clean up our classname concatenation.