|
1 | 1 | # Qwik-UI Coding Standards
|
2 | 2 |
|
| 3 | +## Headless package CSS style |
| 4 | + |
| 5 | +The goal of this pack is to have a minimal style to be used at will. For the documentation of the headless components we will provide minimal examples style to show them to the end users. |
| 6 | + |
3 | 7 | ## Coding practices
|
4 | 8 |
|
| 9 | +- Naming conventions |
| 10 | + |
| 11 | +Use Pascal casing for the component names. For example: |
| 12 | + |
| 13 | +```jsx |
| 14 | +<Select> |
| 15 | +<SelectTrigger> |
| 16 | +<SelectItem> |
| 17 | +``` |
| 18 | + |
| 19 | +Use Signal, Loader, Action postfixes in variable names. For example: |
| 20 | + |
| 21 | +```js |
| 22 | +const myComponentSignal = useSignal(''); |
| 23 | +const myDataLoader = loader.use(); |
| 24 | +const myCmpAction = action.use(); |
| 25 | +``` |
| 26 | + |
| 27 | +Use camel casing for folder names and for file names. For example: |
| 28 | + |
| 29 | +``` |
| 30 | +buttonGroup.tsx |
| 31 | +buttonGroup folder |
| 32 | +``` |
| 33 | + |
| 34 | +###Component conventions |
| 35 | + |
| 36 | +- For each component, add a props interface and declare all the props API there. For example: |
| 37 | + |
| 38 | +```ts |
| 39 | +interface TooltipProps { |
| 40 | + class?: string; |
| 41 | + tip: string; |
| 42 | + type?: ColorTypes; |
| 43 | + position?: Positions; |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +- Use object destructuring in the component$ declaration on all the props you are going to use. For example: |
| 48 | + |
| 49 | +```tsx |
| 50 | +export const Tooltip = component$(({ tip, position = 'top', type, ...props}: TooltipProps) => { |
| 51 | + ... |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +- Try to avoid using `useClientEffect$` function. See [Qwik best practices](https://qwik.builder.io/docs/cheat/best-practices/) for the why. |
| 56 | +- Try to use the Slot element whenever the component can accept children |
| 57 | +- Use array spread on props to allow the component user to send props that override ours. For example: |
| 58 | + |
| 59 | +```tsx |
| 60 | +return ( |
| 61 | + <span {...props}> |
| 62 | + <Slot /> |
| 63 | + </span> |
| 64 | +); |
| 65 | +``` |
| 66 | + |
| 67 | +- Use accessibility attributes whenever they are needed. |
| 68 | +- For simple primitive states use signals. For object state, use stores. |
| 69 | + |
| 70 | +## Example of headless button customisation with Daisy |
| 71 | + |
| 72 | +```tsx |
| 73 | +import { component$, QwikIntrinsicElements, Slot } from '@builder.io/qwik'; |
| 74 | +import { Button as HeadlessButton } from '@qwik-ui/headless'; |
| 75 | +import { clsq } from '@qwik-ui/shared'; |
| 76 | + |
| 77 | +// This type holds all the HTML attributes (disabled, hidden, ... ) |
| 78 | +export type HTMLButtonProps = QwikIntrinsicElements['button']; |
| 79 | +export type DaisyButtonProps = { size?: 'sm' | 'md', ... }; |
| 80 | +export type ButtonProps = HTMLButtonProps & DaisyButtonProps; |
| 81 | + |
| 82 | +export const Button = component$( |
| 83 | + ({ size = 'md', class: classNames, ...rest }: ButtonProps) => { |
| 84 | + const { sizes, ... } = { sizes: { sm: 'btn-sm', md: 'btn-md', ... } }; |
| 85 | + return ( |
| 86 | + <HeadlessButton {...rest} class={clsq('btn', sizes[size], classNames)}> |
| 87 | + <Slot /> |
| 88 | + </HeadlessButton> |
| 89 | + ); |
| 90 | + } |
| 91 | +); |
| 92 | +``` |
0 commit comments