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

RFC: Do not allow CSS shorthands in makeStyles() calls #20573

Merged
merged 5 commits into from Nov 23, 2021

Conversation

layershifter
Copy link
Member

@layershifter layershifter commented Nov 10, 2021

Description of changes

This RFC proposed to forbid usage of CSS shorthands in makeStyles() calls. Check also #19402 for context.


📖 Rendered preview

@layershifter layershifter added the Type: RFC Request for Feedback label Nov 10, 2021
@layershifter layershifter added this to In progress in !!! OLD DO NOT USE!!! v9.0.0 via automation Nov 10, 2021
@size-auditor
Copy link

size-auditor bot commented Nov 10, 2021

Asset size changes

Size Auditor did not detect a change in bundle size for any component!

Baseline commit: c4e248bbe45620352dc76e2b200f9163641c399d (build)

@fabricteam
Copy link
Collaborator

fabricteam commented Nov 10, 2021

📊 Bundle size report

Unchanged fixtures
Package & Exports Size (minified/GZIP)
react-accordion
Accordion (including children components)
57.542 kB
18.215 kB
react-avatar
Avatar
44.893 kB
12.824 kB
react-badge
Badge
22.394 kB
6.707 kB
react-badge
CounterBadge
23.282 kB
7.019 kB
react-badge
PresenceBadge
22.058 kB
6.334 kB
react-button
Button
24.425 kB
7.429 kB
react-button
CompoundButton
29.713 kB
8.383 kB
react-button
MenuButton
26.82 kB
8.193 kB
react-button
SplitButton
33.035 kB
9.293 kB
react-button
ToggleButton
33.554 kB
8.029 kB
react-card
Card - All
49.986 kB
14.787 kB
react-card
Card
45.406 kB
13.558 kB
react-card
CardFooter
7.418 kB
3.182 kB
react-card
CardHeader
8.778 kB
3.64 kB
react-card
CardPreview
7.682 kB
3.341 kB
react-components
react-components: Accordion, Button, FluentProvider, Image, Menu, Popover
169.377 kB
48.544 kB
react-components
react-components: FluentProvider & webLightTheme
32.669 kB
10.652 kB
react-divider
Divider
14.828 kB
5.375 kB
react-image
Image
8.989 kB
3.717 kB
react-input
Input
13.79 kB
4.947 kB
react-label
Label
8.177 kB
3.445 kB
react-link
Link
10.883 kB
4.449 kB
react-make-styles
makeStaticStyles (runtime)
7.59 kB
3.322 kB
react-make-styles
makeStyles + mergeClasses (runtime)
22.345 kB
8.447 kB
react-make-styles
makeStyles + mergeClasses (build time)
2.561 kB
1.206 kB
react-menu
Menu (including children components)
105.323 kB
32.514 kB
react-menu
Menu (including selectable components)
107.711 kB
32.88 kB
react-popover
Popover
101.512 kB
30.816 kB
react-portal
Portal
6.621 kB
2.226 kB
react-positioning
usePopper
22.808 kB
7.935 kB
react-provider
FluentProvider
14.056 kB
5.267 kB
react-slider
RangedSlider
39.807 kB
11.491 kB
react-slider
Slider
33.196 kB
10.312 kB
react-switch
Switch
25.311 kB
8.143 kB
react-text
Text - Default
10.603 kB
4.172 kB
react-text
Text - Wrappers
13.899 kB
4.513 kB
react-theme
Teams: all themes
29.506 kB
6.526 kB
react-theme
Teams: Light theme
18.517 kB
5.251 kB
react-tooltip
Tooltip
44.403 kB
15.365 kB
react-utilities
SSRProvider
189 B
161 B
🤖 This report was generated against c4e248bbe45620352dc76e2b200f9163641c399d

@fabricteam
Copy link
Collaborator

fabricteam commented Nov 10, 2021

Perf Analysis (@fluentui/react)

No significant results to display.

All results

Scenario Render type Master Ticks PR Ticks Iterations Status
Avatar mount 1045 1061 5000
BaseButton mount 1039 1038 5000
Breadcrumb mount 2795 2765 1000
ButtonNext mount 576 558 5000
Checkbox mount 1695 1729 5000
CheckboxBase mount 1490 1464 5000
ChoiceGroup mount 5258 5228 5000
ComboBox mount 1041 1029 1000
CommandBar mount 10847 10850 1000
ContextualMenu mount 8917 8969 1000
DefaultButton mount 1273 1289 5000
DetailsRow mount 4109 4090 5000
DetailsRowFast mount 4166 4175 5000
DetailsRowNoStyles mount 3961 3981 5000
Dialog mount 2769 2801 1000
DocumentCardTitle mount 174 170 1000
Dropdown mount 3607 3570 5000
FluentProviderNext mount 4004 4065 5000
FluentProviderWithTheme mount 239 233 10
FluentProviderWithTheme virtual-rerender 105 99 10
FluentProviderWithTheme virtual-rerender-with-unmount 264 272 10
FocusTrapZone mount 1894 1943 5000
FocusZone mount 1952 1929 5000
IconButton mount 1964 1932 5000
Label mount 388 388 5000
Layer mount 3223 3368 5000
Link mount 528 527 5000
MakeStyles mount 1909 1874 50000
MenuButton mount 1664 1660 5000
MessageBar mount 2140 2150 5000
Nav mount 3691 3633 1000
OverflowSet mount 1188 1232 5000
Panel mount 2693 2607 1000
Persona mount 933 926 1000
Pivot mount 1589 1615 1000
PrimaryButton mount 1460 1417 5000
Rating mount 8809 8809 5000
SearchBox mount 1484 1512 5000
Shimmer mount 2842 2809 5000
Slider mount 2142 2157 5000
SpinButton mount 5473 5390 5000
Spinner mount 456 435 5000
SplitButton mount 3430 3406 5000
Stack mount 566 564 5000
StackWithIntrinsicChildren mount 1890 1931 5000
StackWithTextChildren mount 5353 5289 5000
SwatchColorPicker mount 11320 11337 5000
TagPicker mount 2857 2866 5000
TeachingBubble mount 14036 13941 5000
Text mount 477 483 5000
TextField mount 1566 1581 5000
ThemeProvider mount 1258 1290 5000
ThemeProvider virtual-rerender 644 648 5000
ThemeProvider virtual-rerender-with-unmount 2048 2058 5000
Toggle mount 901 905 5000
buttonNative mount 135 145 5000

Perf Analysis (@fluentui/react-northstar)

Perf tests with no regressions
Scenario Current PR Ticks Baseline Ticks Ratio
TreeWith60ListItems.default 206 181 1.14:1
TextMinimalPerf.default 415 382 1.09:1
ChatDuplicateMessagesPerf.default 362 334 1.08:1
PortalMinimalPerf.default 199 186 1.07:1
AnimationMinimalPerf.default 475 449 1.06:1
ButtonMinimalPerf.default 206 194 1.06:1
FlexMinimalPerf.default 328 308 1.06:1
ToolbarMinimalPerf.default 1073 1017 1.06:1
FormMinimalPerf.default 498 475 1.05:1
LoaderMinimalPerf.default 759 722 1.05:1
ReactionMinimalPerf.default 438 416 1.05:1
AvatarMinimalPerf.default 233 223 1.04:1
DividerMinimalPerf.default 423 405 1.04:1
ListCommonPerf.default 742 713 1.04:1
RefMinimalPerf.default 259 249 1.04:1
CarouselMinimalPerf.default 548 533 1.03:1
DatepickerMinimalPerf.default 6082 5921 1.03:1
ProviderMergeThemesPerf.default 1802 1743 1.03:1
SegmentMinimalPerf.default 407 394 1.03:1
TableManyItemsPerf.default 2174 2119 1.03:1
TooltipMinimalPerf.default 1198 1160 1.03:1
ButtonSlotsPerf.default 633 618 1.02:1
DialogMinimalPerf.default 847 832 1.02:1
DropdownManyItemsPerf.default 786 769 1.02:1
SkeletonMinimalPerf.default 398 392 1.02:1
SplitButtonMinimalPerf.default 4759 4669 1.02:1
AttachmentMinimalPerf.default 183 182 1.01:1
BoxMinimalPerf.default 391 389 1.01:1
ButtonOverridesMissPerf.default 1891 1878 1.01:1
ChatMinimalPerf.default 756 748 1.01:1
ChatWithPopoverPerf.default 433 427 1.01:1
DropdownMinimalPerf.default 3255 3230 1.01:1
EmbedMinimalPerf.default 4617 4558 1.01:1
GridMinimalPerf.default 382 378 1.01:1
ItemLayoutMinimalPerf.default 1381 1367 1.01:1
LayoutMinimalPerf.default 427 421 1.01:1
MenuButtonMinimalPerf.default 1805 1781 1.01:1
SliderMinimalPerf.default 1819 1806 1.01:1
StatusMinimalPerf.default 747 742 1.01:1
CheckboxMinimalPerf.default 2928 2916 1:1
HeaderMinimalPerf.default 406 404 1:1
HeaderSlotsPerf.default 852 853 1:1
RadioGroupMinimalPerf.default 512 513 1:1
TableMinimalPerf.default 439 440 1:1
TextAreaMinimalPerf.default 562 560 1:1
CustomToolbarPrototype.default 4310 4314 1:1
TreeMinimalPerf.default 887 883 1:1
AttachmentSlotsPerf.default 1151 1166 0.99:1
ImageMinimalPerf.default 425 429 0.99:1
ListNestedPerf.default 608 614 0.99:1
MenuMinimalPerf.default 941 948 0.99:1
PopupMinimalPerf.default 627 634 0.99:1
IconMinimalPerf.default 677 686 0.99:1
AlertMinimalPerf.default 313 320 0.98:1
InputMinimalPerf.default 1423 1446 0.98:1
LabelMinimalPerf.default 440 448 0.98:1
ListMinimalPerf.default 576 587 0.98:1
ListWith60ListItems.default 707 721 0.98:1
CardMinimalPerf.default 639 657 0.97:1
RosterPerf.default 1334 1375 0.97:1
ProviderMinimalPerf.default 1190 1228 0.97:1
AccordionMinimalPerf.default 176 184 0.96:1
VideoMinimalPerf.default 678 718 0.94:1

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 10, 2021

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 aac2298:

Sandbox Source
@fluentui/react 8 starter Configuration
@fluentui/react-components 9 starter Configuration

makeStyles({
rootA: { paddingLeft: '4px', paddingRight: '4px', paddingBottom: '4px', paddingTop: '4px' },
rootB: {
...macros.padding('4px'),
Copy link
Member

Choose a reason for hiding this comment

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

For ex-C++ developers, the word macros has a lot of history to it :) How about calling it shorthand(), fui(), makeCss()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it also has overlap with "Babel macros". I will rename it to shorthands.*, that makes more sense to me 👍

- 👍 No CSS shorthands => no problems
- 👍 No new syntax
- 👎 Non obvious TypeScript errors in some cases

Copy link
Member

Choose a reason for hiding this comment

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

I would add a con that we have to explain this to users of makeStyles in our documentation. It causes Fluent to seem a little less aligned with web standards since people would expect to use shorthand with raw CSS and with most frameworks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, true, added 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm afraid I don't have the full context so please take this with a grain of salt, but it doesn't make sense to me that complicating DX should be justified with adding documentation. As a user, I would expect a styling library to allow me to write CSS like I normally would.
Could we maybe contribute to inline-style-expand-shorthand to add the support we need instead or is this what you meant in the issue that it's not feasible in terms of complexity?

Copy link
Member Author

Choose a reason for hiding this comment

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

@andrefcdias I agree, but during offline discussion we agreed to make something always predictable instead of sometimes working. Currently it's sometimes working.


The problem is covered in the summary section of this RFC and in the original issue #19402. There are different cases: in some it's about complexity, but there is the main case when it's about possibility at all:

{ padding: 'var(--some-var)' }

👆 CSS variable can be anything (🗃️ CodeSandbox), it means that it's unsafe to expand it at all.

To summarize:

  • CSS variables are not expandable at all during build time (we don't have CSS variables in Node environment even if we would like to resolve and expand)
  • CSS can be resolved in runtime via calls of getPropertyValue() (🗃️ CodeSandbox), but will be too expensive as it should be done for every component and every usage of theme or CSS variables
    • Note 1: getPropertyValue() should be called after render as during we may not have DOM at all (👋 double rendering)
    • Note 2: we need to handle updates (How at all?)

The same problem is true for any CSS-in-JS engine that uses Atomic CSS (Compiled, Stylex, Fela, etc.).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying! Just wanted to make sure we covered all our options.


- 👍 No CSS shorthands => no problems
- 👍 No new syntax
- 👎 Non obvious TypeScript errors in some cases
Copy link
Member

Choose a reason for hiding this comment

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

Consider if a custom es-lint rule might help to convert shorthand to the macro function or provide better error information.

Copy link
Member Author

Choose a reason for hiding this comment

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

Like this, added 👍

- `borderRadius`
- `borderWidth`
- `margin`
- `padding`
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding background.
Lower priority, it would be a bonus to have a few for common shorthand in flexbox and grid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it's one of the first things that I was thinking, but it's not obvious unfortunately as background value could also image:

// ✅ Easy-peasy
shorthands.background("green");
// ➡️ { backgroundColor: 'green' }

// 💥 Oops
shorthands.background('url("lizard.png")');
// ➡️ { backgroundColor: 'url("lizard.png")' }

https://developer.mozilla.org/en-US/docs/Web/CSS/background


Any ideas?

Copy link
Member

@khmakoto khmakoto left a comment

Choose a reason for hiding this comment

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

Approving! I agree with most of Geoff's comments.

Co-authored-by: Miroslav Stastny <mistastn@microsoft.com>
@layershifter
Copy link
Member Author

  • we discussed this RFC in offline (during Tech sync) ✅
  • this PR has enough approvals ✅
  • it stays open for 2 weeks ✅

So I am merging it. If there will be any ideas how we could solve in a better way - we can always re-consider implementation and the decision.

@layershifter layershifter merged commit 4baa29e into master Nov 23, 2021
!!! OLD DO NOT USE!!! v9.0.0 automation moved this from In progress to Done Nov 23, 2021
@layershifter layershifter deleted the rfc/no-css-shorthands-in-make-styles branch November 23, 2021 09:41
@ling1726 ling1726 mentioned this pull request Dec 7, 2021
2 tasks
mlp73 pushed a commit to mlp73/fluentui that referenced this pull request Jan 17, 2022
…0573)

* RFC: Do not allow CSS shorthands in `makeStyles()` calls

* rename "macros" to "shorthands"

* add one more con

* add note about ESLint rules

* Update rfcs/convergence/no-css-shorthands-in-make-styles.md

Co-authored-by: Miroslav Stastny <mistastn@microsoft.com>

Co-authored-by: Miroslav Stastny <mistastn@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

6 participants