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

feat: Add style transform support for stencils #2489

Merged

Conversation

NicholasBoll
Copy link
Member

@NicholasBoll NicholasBoll commented Dec 21, 2023

Summary

Fixes: #2463

  • Refactors the style transform to make it easier to understand the parts.
  • Add automated fallback support
  • Removed unnecessary fallbacks
  • Add style transform support for createStencils
  • Add auto-wrapping for CSS variables in createStyles, createStencils, and the transformer.

Release Category

Components

Release Note

CSS variables can now be unwrapped anywhere in a property definition when using createStyles or createStencil. Previously, unwrapped variables could only be used if they started the property. For example, the following is now valid:

const myVar = '--my-var';

const myStyles= createStyles({
  boxShadow: `0 0 0 2px ${myVar}`
})

You no longer need to use cssVar around myVar to be valid. This should help with the transition from hard-coded variables (const myVar = 'red' => const myVar = '--my-var') without needing to wrap in a cssVar. It allows us to think about myVar as a JS variable and the system will convert to a CSS variable for us.


Checklist

For the Reviewer

  • PR title is short and descriptive
  • PR summary describes the change (Fixes/Resolves linked correctly)

Where Should the Reviewer Start?

Areas for Feedback? (optional)

  • Code
  • Documentation
  • Testing

Testing Manually

Tests should suffice, but you could always do a (cd modules/react; yarn clean && yarn build:es6) and verify the dist directory. Remember to do a yarn clean when you're done otherwise your environment is hosed.

@NicholasBoll NicholasBoll added the ready for review Code is ready for review label Dec 21, 2023
@github-actions github-actions bot requested a review from alanbsmith December 21, 2023 23:47
Copy link

cypress bot commented Dec 22, 2023

Passing run #6646 ↗︎

0 964 3 0 Flakiness 0
⚠️ You've recorded test results over your free plan limit.
Upgrade your plan to view test results.

Details:

Merge 43061c7 into bd715f1...
Project: canvas-kit Commit: f928e200cd ℹ️
Status: Passed Duration: 16:43 💡
Started: Jan 5, 2024 9:31 PM Ended: Jan 5, 2024 9:48 PM

Review all test suite changes for PR #2489 ↗︎

@NicholasBoll NicholasBoll linked an issue Jan 5, 2024 that may be closed by this pull request
color: cssVar(brand.error.base, '#de2e21'),
color: brand.error.base,
Copy link
Member Author

Choose a reason for hiding this comment

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

This type of change is in many places. Auto-fallback works and manually including a fallback is a maitenance issue.

Copy link
Contributor

@mannycarrera4 mannycarrera4 left a comment

Choose a reason for hiding this comment

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

You write code, sometimes I agree with the code you write


let boxShadow: ts.TemplateExpression;
switch (inset) {
case 'outer':
Copy link
Contributor

Choose a reason for hiding this comment

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

Would outer be considered to be both focus rings on the "outside" of the container? I noticed an inset here or this something we need to talk about when re-factoring focusRing?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is why I think focusRing will need an update. outer is actually both inner:

* Specifies where the ring(s) should be attached.
* - undefined: Both "inner" and "outer" shadows outside the container.
* - 'inner': "Inner" shadow inset. "Outer" shadow outside the container.
* - 'outer': Both "inner" and "outer" shadows inside the container.

I'm supporting focusRing in the static optimizer, not changing it.

@NicholasBoll NicholasBoll added automerge and removed ready for review Code is ready for review labels Jan 8, 2024
@alanbsmith alanbsmith enabled auto-merge (squash) January 8, 2024 22:37
@alanbsmith alanbsmith disabled auto-merge January 8, 2024 22:37
@alanbsmith alanbsmith enabled auto-merge (squash) January 8, 2024 22:37
@alanbsmith alanbsmith merged commit 99b5f95 into Workday:prerelease/minor Jan 8, 2024
15 checks passed
@NicholasBoll NicholasBoll deleted the fix/style-transformer-stencil branch January 16, 2024 20:25
@alanbsmith alanbsmith mentioned this pull request Jan 18, 2024
10 tasks
alanbsmith pushed a commit that referenced this pull request Feb 7, 2024
#2489 updated style transform to support stencils, but also allowed other style utilities to be transformed outside a `createStyles` or `createStencil` call. Since `createStyles` and `createStencil` have strict requirements for statically analyzable values and other places do not, this can result in odd behavior. For example, calling the following is an incorrect transformation, but present in `Checkbox.tsx`:

```tsx
      ...focusRing({
        width: variant === 'inverse' ? 2 : 0,
        separation: 0,
        animate: false,
        innerColor: variant === 'inverse' ? colors.blackPepper400 : undefined,
        outerColor: variant === 'inverse' ? colors.frenchVanilla100 : undefined,
      }),
```

This will result in the following:
```tsx
   ... { boxShadow: variant === `0 0 0 ${2} ${variant === 'inverse' ? colors.blackPepper400 : undefined}, ...`
```

Since `focusRing` allows `undefined` as an argument with a default fallback, this works at runtime, but the transform doesn't understand the context of runtime. There's no strict check outside static style functions, so the fix is to only run this transform from inside a static style function.

[category:Components]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add style transformation support for stencils
4 participants