-
Notifications
You must be signed in to change notification settings - Fork 45
Coding conventions
Eirik Backer edited this page Jan 20, 2025
·
3 revisions
To make it easier to contribute and for ourselves to have a consistent codebase, we should document some of our coding conventions.
- Always provide ref through
forwardRef(unless exposing a Context) - Always check props naming conventions on other components for consistency
- Add all HTML props through
React.{TagName}HTMLAttributes<HTML{TagName}Element> - Add the HTML attributes on the outermost element
- Avoid custom use of native HTML attributes (i.e.
classNameshould always work as expected) - Avoid invalid states (i.e. both
successTextanderrorTextat the same time) - Avoid creating multiple components when they can be just variant or compound with same functionality
- Avoid reassigning CSS custom properties: ❌ :
--bg: var(--bg-hover)vs ✅:background: var(--bg-hover) - Use
useId()when if needed for accessibility - Use
@media(hover: hover) and (pointer: fine)around:hoverto avoid:hoveron touch-devices - Use compound components pattern instead of many props on single component
- Use lowercase boolean props (i.e. hidden instead of
isHidden) - Use
:focus-visibleto style focus state - Use
ReactNodetype for props instead of string (unless it has to be string) - Use
useEffectif manipulating DOM directly to ensure server compatibility - Use
Componentas name of the outermost HTML element rendering the component - Use
Component.Contextas a wrapper (andComponent.Trigger) if a component has context and trigger - Use
Contextas name of internal, non-exposed Context - Use import of specific React typescript types, instead of depending on global
React.* - Use
Component.Listwhen listing multiple items - Use
backgroundand notbackground-colorin tokens to allow setting advanced values likelinear-gradient - Use
&:not([hidden]) { display: }arounddisplayto supporthiddenattribute - Use
--dscprefix for component tokens/variables - Use CSS property name in component tokens its applied to (ie
--dsc-button-padding-block)