-
Notifications
You must be signed in to change notification settings - Fork 2.9k
rfc: "No action" / default property value standardization #23304
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
Merged
theerebuss
merged 11 commits into
microsoft:master
from
theerebuss:rfc-standardize-no-action-prop-values
Jul 13, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a44d78f
feat: add new RFC
theerebuss 1ca1378
chore: remove `null` values from proposal
theerebuss c46c4a2
chore: add proposal to fix lookup pattern con
theerebuss e707f26
chore: add code examples
theerebuss 81fde29
chore: add unifying of shape pro
theerebuss 226b1e6
chore: a nice rephrasing
theerebuss 3dc1878
Update rfcs/react-components/no-action-prop-value-standardization.md
theerebuss 7adbf1f
chore: fix usage
theerebuss 987a00a
chore: simplify proposal
theerebuss e37d801
chore: remove mentions of hook changes
theerebuss 702650b
chore: remove implementation proposal
theerebuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
rfcs/react-components/no-action-prop-value-standardization.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # RFC: "No action" / default property value standardization | ||
|
|
||
| --- | ||
|
|
||
| _@andrefcdias @Hotell_ | ||
|
|
||
| ## Summary | ||
|
|
||
| This RFC aims to standardize what values we use in our components for cases where a prop value results in no action, i.e. defaults that apply no styles. | ||
|
|
||
| ## Background | ||
|
|
||
| Currently, the approach we follow for all components is to use a string value like `'off'`, `'none'` or `'default'` for default values of a prop. This happens both on cases where the default prop has and doesn't have an effect on the component. | ||
|
|
||
| ## Problem statement | ||
|
|
||
| There is no standardization for the naming, resulting in our users needing to read documentation to figure out what to use, as the names used might be compared to CSS keywords (like `'unset'`), and requiring specific component knowledge as the user can't reuse this information for other components. It is also misleading to provide a string value that has no actual impact on the component. | ||
|
|
||
| ## Detailed Design or Proposal | ||
|
|
||
| This RFC proposes that we leverage the standard JavaScript default, `undefined`, for attributes instead of a string. | ||
|
|
||
| Put simply, when a component has a default state/behavior, it SHOULD HAVE a default value.<br/> | ||
| Example: Card.appearance has `'filled' | 'filled-alternative' | 'outline' | 'subtle'` and is `'filled'` by default. | ||
|
|
||
| When a component does not have a default state/behavior, it SHOULD NOT HAVE a default value.<br/> | ||
| Anti-example (current state): Text.font has `'base' | 'monospace' | 'numeric'` and defaults to `'base'`, where `'base'` does not apply any styles<br/> | ||
| Example (proposal): Text.font has `'monospace' | 'numeric'` and has no defaults (i.e. `undefined`) | ||
|
|
||
| ### Usage | ||
|
|
||
| #### With default | ||
|
|
||
| ```tsx | ||
| // Card.types.ts | ||
| type CardProps = { | ||
| appearance?: 'filled' | 'filled-alternative' | 'outline' | 'subtle'; | ||
| }; | ||
| type CardState = { | ||
| // Required as we need a value for our hooks to work | ||
| appearance: 'filled' | 'filled-alternative' | 'outline' | 'subtle'; | ||
| }; | ||
|
|
||
| // useCard.ts | ||
| const { appearance = 'filled' /* {...} */ } = props; // Default applied to enforce behavior | ||
|
|
||
| const state = { | ||
| appearance, | ||
| // {...} | ||
| }; | ||
|
|
||
| // useCardStyles.ts | ||
| const appearanceLookup = { | ||
| filled: styles.filled, | ||
| 'filled-alternative': styles.filledAlternative, | ||
| outline: styles.outline, | ||
| subtle: styles.subtle, | ||
| } as const; | ||
|
|
||
| state.root.className = mergeClasses( | ||
| cardClassNames.root, | ||
| styles.root, | ||
| appearanceLookup[state.appearance], | ||
| // {...} | ||
| state.root.className, | ||
| ); | ||
| ``` | ||
|
|
||
| #### Without default | ||
|
|
||
| ```tsx | ||
| // Text.types.ts | ||
| type TextProps = { | ||
| font?: 'monospace' | 'numeric'; | ||
| }; | ||
| type TextState = { | ||
| // Also nullable as the default does not overwrite styles | ||
| font?: 'monospace' | 'numeric'; | ||
| }; | ||
|
|
||
| // useText.ts | ||
| const { font /* {...} */ } = props; // We no longer set a default here | ||
|
|
||
| const state = { | ||
| font, | ||
| // {...} | ||
| }; | ||
| ``` | ||
|
|
||
| ### Usage differences | ||
|
|
||
| #### Dynamically setting a prop value | ||
|
|
||
| ```jsx | ||
| // Before | ||
| <Text font={isNumeric ? 'numeric' : 'base'}> | ||
|
|
||
| // After | ||
| <Text font={isNumeric ? 'numeric' : undefined}> | ||
| ``` | ||
|
|
||
| ## Pros and Cons | ||
|
|
||
| ### Pros | ||
|
|
||
| - Follows JavaScript standards | ||
| - Intuitive to the user because of the above pro - no need to read documentation | ||
| - Consistency across our product - enables ease of use | ||
| - Unified `Props` and `State` shape | ||
|
|
||
| ### Cons | ||
|
|
||
| - Using the lookup object pattern for styling will require changes as `undefined` can't be used as an index type | ||
| - Dynamically setting a property value requires explicit usage of `undefined`: | ||
| ```jsx | ||
| <Text font={isNumeric ? 'numeric' : undefined}> | ||
| ``` | ||
|
|
||
| ## Discarded Solutions | ||
|
|
||
| ### Standardizing the string label used for default values | ||
|
|
||
| Using a specific keyword for all possible scenarios would be difficult as there isn't a word that is neutral and appropriate for all the different cases. | ||
|
|
||
| - `none` can be confused with CSS's `none` | ||
| - `off` does not fit cases like an `align` property | ||
| - `default` is ambiguous and does not convey the fact that nothing happens | ||
|
|
||
| Even if we can get a word that fits all the cases, the user would still need to add every single property when consuming a styles hook, like the example shown in the [Problem Statement](##Problem_statement). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.