Skip to content

Commit

Permalink
Update package installation instructions and fix class variant proper…
Browse files Browse the repository at this point in the history
…ty prefixing
  • Loading branch information
1aron committed Feb 29, 2024
1 parent 79436e8 commit 61dd435
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ It will be rendered as:
```

## Getting Started
Install the package `class-variant`, `@master/styled.react`, or `@master/styled.vue` depending on your framework:
Install the package `npm i class-variant`, `npm i @master/styled.react`, or `npm i @master/styled.vue` depending on your framework:

### Vanilla JS
```js
Expand Down Expand Up @@ -133,25 +133,27 @@ return (
Predefine the class variant corresponding to the property.
```tsx
const Button = styled.button('flex', {
size: {
$size: {
sm: 'font:12 size:8x',
md: 'font:14 size:12x'
},
disabled: 'opacity:.5',
$disabled: 'opacity:.5',
...
})

return (
<Button size="sm" disabled />
<Button $size="sm" disabled />
)
```
```html
<button class="flex font:12 size:8x opacity:.5" disabled></button>
```
> ⚠️ Custom properties that are not element-owned properties must be prefixed with `$prop`, otherwise they will be reflected on the final element and an error may be thrown.
You can set default properties for elements.
```tsx
Button.defaultProps = {
size: 'md'
$size: 'md'
}

return (
Expand All @@ -166,11 +168,11 @@ return (
Dynamically apply class names based on function properties.
```tsx
const Element = styled.div('fg:white',
({ color }) => color && `bg:${color}`
({ $color }) => $color && `bg:${$color}`
)

return (
<Element color="blue" />
<Element $color="blue" />
)
```
```html
Expand All @@ -181,13 +183,13 @@ return (
Applies the target class name matching all specified property keys and their values.
```tsx
const Button = styled.button('inline-flex',
['uppercase', { intent: 'primary', size: 'md' }]
['uppercase', { $intent: 'primary', $size: 'md' }]
)

return (
<Button intent="primary">Not matched</button>
<Button size="md">Not matched</button>
<Button intent="primary" size="md">Matched</button>
<Button $intent="primary">Not matched</button>
<Button $size="md">Not matched</button>
<Button $intent="primary" $size="md">Matched</button>
)
```
```html
Expand Down
4 changes: 2 additions & 2 deletions packages/react/e2e/ApplyBasedOnMatchingProperties.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { test, expect } from '@playwright/experimental-ct-react'
import Element from './ApplyBasedOnMatchingProperties'

test('element', async ({ mount }) =>
await expect(await mount(<Element intent="primary" size="md" />))
await expect(await mount(<Element $intent="primary" $size="md" />))
.toHaveClass('inline-flex uppercase')
)

test('element without uppercase', async ({ mount }) =>
await expect(await mount(<Element intent="primary" />))
await expect(await mount(<Element $intent="primary" />))
.not.toHaveClass('uppercase')
)
2 changes: 1 addition & 1 deletion packages/react/e2e/ApplyBasedOnMatchingProperties.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from '../src';

const Element = styled.button('inline-flex',
['uppercase', { intent: 'primary', size: 'md' }]
['uppercase', { $intent: 'primary', $size: 'md' }]
)

export default Element

0 comments on commit 61dd435

Please sign in to comment.