Skip to content

Commit a0cefe3

Browse files
committed
fix: use div as default tag
1 parent bb37cbc commit a0cefe3

4 files changed

Lines changed: 65 additions & 14 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 [these people](https://github.com/tw-in-js/twind-content/graphs/contributors)
3+
Copyright (c) 2021 [these people](https://github.com/tw-in-js/twind-react/graphs/contributors)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,73 @@ yarn add @twind/react
2626

2727
> Please see [twind/style](https://twind.dev/docs/modules/twind_style.html) for config examples.
2828
29+
[![Try this example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/twind-react-styled-90y9n?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.js&theme=dark)
30+
2931
```jsx
30-
const Button = styled('button', {
31-
base: 'text-base',
32+
import { styled } from "@twind/react"
33+
34+
const Box = styled()
35+
36+
const Button = styled("button", {
37+
base: `
38+
appearance-none border-none bg-transparent
39+
rounded-full px-2.5
40+
`,
41+
3242
variants: {
3343
size: {
34-
sm: `text-sm`,
35-
lg: `text-lg`,
44+
sm: `text-sm h-6`,
45+
md: `text-base h-9`,
46+
},
47+
48+
variant: {
49+
gray: `
50+
bg-gray-500
51+
hover:bg-gray-600
52+
`,
53+
primary: `
54+
text-white bg-purple-500
55+
hover:bg-purple-600
56+
`,
3657
},
58+
outlined: {
59+
true: `bg-transparent ring-1`,
60+
},
61+
},
62+
63+
defaults: {
64+
variant: "gray",
65+
size: "sm",
3766
},
67+
68+
matches: [
69+
{
70+
variant: "gray",
71+
outlined: true,
72+
use: `ring-gray-500`,
73+
},
74+
{
75+
variant: "primary",
76+
outlined: true,
77+
use: `text-purple-500 ring-gray-500 hover:text-white`,
78+
},
79+
],
3880
})
3981

40-
<Button>Click Me</Button>
41-
<Button size="lg">Click Me</Button>
82+
<Box tw="m-2.5 flex flex-wrap" css={{ gap: "20px" }}>
83+
<Button>Button</Button>
84+
<Button variant="gray">Gray Button</Button>
85+
<Button variant="primary">Primary Button</Button>
86+
<Button variant="gray" outlined>
87+
Outlined Gray Button
88+
</Button>
89+
<Button variant="primary" outlined>
90+
Outlined Primary Button
91+
</Button>
92+
<Button variant="primary" outlined size={{ initial: "sm", lg: "md" }}>
93+
Responsive Primary Button
94+
</Button>
95+
</Box>
4296
```
4397

4498
## License

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ export function styled<Variants, BaseVariants, Tag extends React.ElementType>(
8585
config?: StyledOptions & StyleConfig<Variants, BaseVariants>,
8686
): StyledComponent<BaseVariants & Variants, Tag> & string
8787

88-
export function styled<Variants, Tag extends React.ElementType>(
88+
export function styled<Variants, Tag extends React.ElementType = 'div'>(
8989
this: TW | null | undefined | void,
9090
tag?: Tag,
9191
config?: StyledOptions & StyleConfig<Variants>,
9292
): StyledComponent<Variants, Tag> & string
9393

9494
export function styled<Tag extends React.ElementType, Variants>(
9595
this: TW | null | undefined | void,
96-
tag: Tag = 'span' as Tag,
96+
tag: Tag = 'div' as Tag,
9797
{ shouldForwardProp = isPropValid, ...config }: StyledOptions & StyleConfig<Variants> = {},
9898
) {
9999
const tw = this || defaultTW
@@ -114,7 +114,7 @@ export function styled<Tag extends React.ElementType, Variants>(
114114

115115
const sc = React.forwardRef(
116116
<T extends React.ElementType = Tag>(
117-
{ as = tag as unknown as T, ...props }: PolymorphicPropsWithRef<StyleProps<Variants>, T>,
117+
{ as = (tag as unknown) as T, ...props }: PolymorphicPropsWithRef<StyleProps<Variants>, T>,
118118
ref: React.ForwardedRef<any>,
119119
) =>
120120
React.createElement(as, {

test.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ test('styled', ({ sheet, styled }) => {
4949
</Button>,
5050
)
5151

52-
assert.is(
53-
markup,
54-
'<button type="button" class="tw-xz6gsm tw-jo04kc x">Click Me</button>',
55-
)
52+
assert.is(markup, '<button type="button" class="tw-xz6gsm tw-jo04kc x">Click Me</button>')
5653

5754
assert.equal(sheet.target, ['.tw-xz6gsm{font-size:1.125rem;line-height:1.75rem}'])
5855
// const ExtendedButton = styled(Button, {

0 commit comments

Comments
 (0)