Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit fb62b5b

Browse files
Merge pull request #12 from chakra-ui/feat/css-reset
Feat/css reset and button
2 parents f5fc776 + af45ecf commit fb62b5b

File tree

21 files changed

+590
-51
lines changed

21 files changed

+590
-51
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,38 @@ yarn install
1414
yarn build
1515
```
1616

17+
### Bootstrap and link packages
18+
```bash
19+
yarn bootstrap
20+
```
21+
1722
### Component playground (based on vite)
1823
```bash
1924
yarn dev
2025
```
2126

27+
## Development Guide
28+
### 1. Creating new components
29+
Chakra UI Vue uses [hygen](https://www.hygen.io/) to generate new components. The component templates can be found in the `_templates/generator/component` directory
30+
31+
```bash
32+
yarn hygen component --name <COMPONENT_NAME> --description="MY_COMPONENT_DESCRIPTION"
33+
```
34+
35+
This creates a new package with the name `<COMPONENT_NAME>` with some basic sanity tests.
36+
37+
After creating your new package, run `yarn workspace @chakra-ui/COMPONENT_NAME && yarn bootstrap` to build and link your component in the monorepo.
38+
39+
Run `yarn dev` to view your new component in the playground.
40+
41+
**Additional notes:**
42+
Add a script for your package workspace in the `package.json` file.
43+
2244
### Major todos:
2345
- [ ] Documentation (to be based on Nuxt 3)
2446
- [ ] Accessibility JS hooks (Documented in Roadmap)
2547

48+
2649
#### Contributors' note:
2750
Hi!
2851

_templates/generator/component/package.json.ejs.t

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ to: packages/<%=h.changeCase.paramCase(name)%>/package.json
1010
"module": "dist/esm/index.js",
1111
"types": "dist/types/index.d.ts",
1212
"typings": "dist/types/index.d.ts",
13-
"author": "Jonathan Bakebwa <codebender828@gmail.com>",
14-
"homepage": "https://github.com/chakra-ui/chakra-ui-vue-next#readme",
15-
"license": "MIT",
16-
"files": [
17-
"dist"
18-
],
1913
"exports": {
2014
".": {
2115
"require": "./dist/cjs/index.js",
2216
"default": "./dist/esm/index.js"
2317
}
2418
},
19+
"author": "Jonathan Bakebwa <codebender828@gmail.com>",
20+
"homepage": "https://github.com/chakra-ui/chakra-ui-vue-next#readme",
21+
"license": "MIT",
22+
"files": [
23+
"dist"
24+
],
25+
2526
"publishConfig": {
2627
"access": "public"
2728
},

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"system": "yarn workspace @chakra-ui/vue-system",
2828
"theme": "yarn workspace @chakra-ui/vue-theme",
2929
"nuxt": "yarn workspace @chakra-ui/nuxt-next",
30-
"test-utils": "yarn workspace @chakra-ui/vue-test-utils"
30+
"test-utils": "yarn workspace @chakra-ui/vue-test-utils",
31+
"c-reset": "yarn workspace @chakra-ui/c-reset"
3132
},
3233
"license": "MIT",
3334
"private": true,

packages/c-button/examples/base-button.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<template>
22
<div>
3-
<c-button class="my-other-class"> Base button ◻️ </c-button>
3+
<c-button mr="3" color-scheme="blue"> Base button</c-button>
4+
<c-button mr="3" color-scheme="green"> Base button</c-button>
5+
<c-button mr="3" color-scheme="orange"> Base button</c-button>
6+
<c-button mr="3" color-scheme="pink"> Base button</c-button>
47
</div>
58
</template>
69

710
<script lang="ts">
8-
import CButton from '@chakra-ui/c-button/src'
11+
import { CButton } from '@chakra-ui/c-button/src'
912
import { defineComponent } from 'vue'
1013
1114
export default defineComponent({
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<template>
2+
<c-button mr="3" size="xs" color-scheme="teal"> Tiny button </c-button>
3+
<c-button mr="3" size="sm" color-scheme="teal"> Small button </c-button>
4+
<c-button mr="3" size="md" color-scheme="teal"> Medium button </c-button>
5+
<c-button mr="3" size="lg" color-scheme="teal"> Large button </c-button>
6+
</template>
7+
8+
<script lang="ts">
9+
import { CButton } from '@chakra-ui/c-button/src'
10+
import { defineComponent } from 'vue'
11+
12+
export default defineComponent({
13+
components: { CButton },
14+
})
15+
</script>

packages/c-button/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
"build": "concurrently yarn:build:*",
1717
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts -d dist/esm --source-maps",
1818
"build:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts -d dist/cjs --source-maps",
19-
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types"
19+
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
20+
"watch": "concurrently yarn:watch:*",
21+
"watch:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts -d dist/esm --source-maps --watch",
22+
"watch:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts -d dist/cjs --source-maps --watch",
23+
"watch:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types --watch"
2024
},
2125
"dependencies": {
26+
"@chakra-ui/styled-system": "^1.4.1",
2227
"@chakra-ui/vue-system": "*"
2328
}
2429
}

packages/c-button/src/button.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { h, defineComponent, PropType, computed } from 'vue'
2+
import {
3+
chakra,
4+
DOMElements,
5+
useStyleConfig,
6+
ThemingProps,
7+
} from '@chakra-ui/vue-system'
8+
import {
9+
SystemCSSProperties,
10+
SystemStyleObject,
11+
} from '@chakra-ui/styled-system'
12+
import { Colors } from '@chakra-ui/vue-theme/dist/types/foundations/colors'
13+
import { ComponentThemeConfig } from '@chakra-ui/vue-theme'
14+
import { dataAttr } from '@chakra-ui/vue-utils'
15+
16+
type ButtonTypes = 'button' | 'reset' | 'submit'
17+
export type ButtonSizes = 'xs' | 'sm' | 'md' | 'lg'
18+
export type ButtonVariants =
19+
| 'subtle'
20+
| 'solid'
21+
| 'outline'
22+
| 'ghost'
23+
| 'link'
24+
| string
25+
26+
const props = {
27+
as: {
28+
type: String as PropType<DOMElements>,
29+
default: 'button',
30+
},
31+
isLoading: Boolean as PropType<boolean>,
32+
isActive: Boolean as PropType<boolean>,
33+
isDisabled: Boolean as PropType<boolean>,
34+
loadingText: String as PropType<string>,
35+
isFullWidth: Boolean as PropType<boolean>,
36+
type: String as PropType<ButtonTypes>,
37+
leftIcon: String as PropType<string>,
38+
rightIcon: String as PropType<string>,
39+
colorScheme: String as PropType<keyof Colors>,
40+
variant: {
41+
type: String as PropType<ButtonVariants>,
42+
default: 'solid',
43+
},
44+
size: {
45+
type: String as PropType<ButtonSizes>,
46+
default: 'md',
47+
},
48+
styleConfig: String as PropType<ComponentThemeConfig>,
49+
50+
/** Not sure if the SystemCSSProperties is the right prop type for this */
51+
iconSpacing: {
52+
type: [String, Number, Array] as PropType<
53+
SystemCSSProperties['marginRight']
54+
>,
55+
default: '0.5rem',
56+
},
57+
}
58+
59+
/** TODO: How to get component props in typescript */
60+
61+
const CButton = defineComponent({
62+
name: 'CButton',
63+
props,
64+
setup(props, { attrs, slots }) {
65+
const themingProps = computed<ThemingProps>(() => ({
66+
colorScheme: props.colorScheme,
67+
variant: props.variant,
68+
size: props.size,
69+
styleConfig: props.styleConfig,
70+
}))
71+
72+
const styles = useStyleConfig('Button', themingProps.value)
73+
74+
const buttonStyles: SystemStyleObject = {
75+
display: 'inline-flex',
76+
appearance: 'none',
77+
alignItems: 'center',
78+
justifyContent: 'center',
79+
transition: 'all 250ms',
80+
userSelect: 'none',
81+
position: 'relative',
82+
whiteSpace: 'nowrap',
83+
verticalAlign: 'middle',
84+
outline: 'none',
85+
width: props.isFullWidth ? '100%' : 'auto',
86+
...styles.value,
87+
}
88+
89+
return () =>
90+
h(
91+
chakra(props.as, 'button'),
92+
{
93+
disabled: props.isDisabled || props.isLoading,
94+
type: props.as === 'button' ? undefined : props.type,
95+
dataActive: dataAttr(props.isActive),
96+
dataLoading: dataAttr(props.isLoading),
97+
...buttonStyles,
98+
...attrs,
99+
},
100+
slots
101+
)
102+
},
103+
})
104+
105+
export default CButton

packages/c-button/src/index.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1 @@
1-
import { h, defineComponent, PropType } from 'vue'
2-
import { chakra, DOMElements } from '@chakra-ui/vue-system'
3-
4-
const CButton = defineComponent({
5-
name: 'CButton',
6-
props: {
7-
as: {
8-
type: String as PropType<DOMElements>,
9-
default: 'button',
10-
},
11-
},
12-
setup(_, { attrs, slots }) {
13-
return () =>
14-
h(
15-
chakra('button', 'button'),
16-
{
17-
...attrs,
18-
},
19-
slots
20-
)
21-
},
22-
})
23-
24-
export default CButton
1+
export { default as CButton } from './button'

packages/c-button/tests/__snapshots__/c-button.test.ts.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
exports[`should render properly 1`] = `
44
<DocumentFragment>
55
<button
6-
class="chakra-button css-0"
7-
/>
6+
class="chakra-button css-myeq6i"
7+
>
8+
Happy button
9+
</button>
810
</DocumentFragment>
911
`;
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1-
import { render } from '../../test-utils/src'
2-
import CButton from '../src'
1+
import { render, userEvent } from '../../test-utils/src'
2+
import { CButton } from '../src'
3+
4+
const renderComponent = (props?: any) => {
5+
const base = {
6+
components: {
7+
CButton,
8+
},
9+
template: '<CButton>Happy button</CButton>',
10+
...props,
11+
}
12+
return render(base)
13+
}
314

415
it('should render properly', () => {
5-
const { asFragment } = render(CButton)
16+
const { asFragment } = renderComponent()
617
expect(asFragment()).toMatchSnapshot()
718
})
19+
20+
it('should be in the DOM', () => {
21+
const { baseElement } = renderComponent()
22+
expect(baseElement).toBeInTheDocument()
23+
})
24+
25+
it('should emit click event on click', async () => {
26+
const handleClick = jest.fn()
27+
const { getByTestId } = renderComponent({
28+
template:
29+
'<c-button data-testid="button" @click="handleClick">Happy button</c-button>',
30+
setup() {
31+
return {
32+
handleClick,
33+
}
34+
},
35+
})
36+
37+
await userEvent.click(getByTestId('button'))
38+
expect(handleClick).toHaveBeenCalled()
39+
})

0 commit comments

Comments
 (0)