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

Commit b98fd81

Browse files
committed
feat: add helper function to provide types for plugin options
1 parent b0e8b45 commit b98fd81

File tree

20 files changed

+185
-67
lines changed

20 files changed

+185
-67
lines changed

components.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* Typescript support for @@chakra-ui/vue-next1.0.0-alpha.5 auto-imported
2+
* Typescript support for @@chakra-ui/vue-next1.0.0-alpha.6 auto-imported
33
* components using `vite-plugin-components`
44
*
55
* @see: https://github.com/antfu/vite-plugin-components#typescript
66
*
77
* This is a generated file. Do not edit it's contents.
88
*
9-
* This file was generated on 2021-07-06T09:19:34.498Z
9+
* This file was generated on 2021-07-16T08:32:18.292Z
1010
*/
1111

1212
import { ChakraProps } from '@chakra-ui/vue-system'

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"vite-plugin-components": "^0.8.3",
143143
"vite-plugin-mdx-vue": "^1.1.3",
144144
"vite-plugin-pages": "^0.9.2",
145+
"vite-plugin-vue-layouts": "^0.3.1",
145146
"vite-ssg": "^0.11.4",
146147
"vitepress": "^0.12.0",
147148
"vue": "^3.1.4",

packages/c-button/src/button.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import {
44
useStyleConfig,
55
ThemingProps,
66
SystemStyleObject,
7+
ComponentWithProps,
8+
DeepPartial,
79
} from '@chakra-ui/vue-system'
810
import { dataAttr, filterUndefined, mergeWith } from '@chakra-ui/utils'
911
import { useButtonGroup } from './button-group'
1012
import { CIcon } from '@chakra-ui/c-icon'
1113
import { CSpinner } from '@chakra-ui/c-spinner'
12-
import { BUTTON_PROPS } from './button.utils'
14+
import { ButtonProps, BUTTON_PROPS } from './button.utils'
1315

1416
const CButtonSpinner = defineComponent({
1517
name: 'CButtonSpinner',
@@ -76,7 +78,7 @@ const CButtonIcon = defineComponent({
7678
* such as submitting a form, opening a dialog, canceling
7779
* an action, or performing a delete operation.
7880
*/
79-
const CButton = defineComponent({
81+
const CButton: ComponentWithProps<DeepPartial<ButtonProps>> = defineComponent({
8082
name: 'CButton',
8183
props: BUTTON_PROPS,
8284
setup(props, { attrs, slots }) {

packages/c-button/src/button.utils.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
import { SystemCSSProperties } from '@chakra-ui/styled-system'
22
import { DOMElements } from '@chakra-ui/vue-system'
3-
import { vueThemingProps } from '@chakra-ui/vue-utils'
3+
import { vueThemingProps, BaseThemedComponentProps } from '@chakra-ui/vue-utils'
44
import { PropType } from 'vue'
55

66
type ButtonTypes = 'button' | 'reset' | 'submit'
77

8+
export interface ButtonProps extends BaseThemedComponentProps {
9+
isLoading?: boolean
10+
isDisabled?: boolean
11+
isActive?: boolean
12+
loadingText?: string
13+
isFullWidth?: boolean
14+
type?: ButtonTypes
15+
leftIcon?: string
16+
rightIcon?: string
17+
iconSpacing?: SystemCSSProperties['marginRight']
18+
}
19+
820
export const BUTTON_PROPS = {
921
as: {
10-
type: String as PropType<DOMElements>,
22+
type: String as PropType<ButtonProps['as']>,
1123
default: 'button',
1224
},
13-
isLoading: Boolean as PropType<boolean>,
14-
isActive: Boolean as PropType<boolean>,
15-
isDisabled: Boolean as PropType<boolean>,
16-
loadingText: String as PropType<string>,
17-
isFullWidth: Boolean as PropType<boolean>,
18-
type: String as PropType<ButtonTypes>,
19-
leftIcon: String as PropType<string>,
20-
rightIcon: String as PropType<string>,
25+
isLoading: Boolean as PropType<ButtonProps['isLoading']>,
26+
isActive: Boolean as PropType<ButtonProps['isActive']>,
27+
isDisabled: Boolean as PropType<ButtonProps['isDisabled']>,
28+
loadingText: String as PropType<ButtonProps['loadingText']>,
29+
isFullWidth: Boolean as PropType<ButtonProps['isFullWidth']>,
30+
type: String as PropType<ButtonProps['type']>,
31+
leftIcon: String as PropType<ButtonProps['leftIcon']>,
32+
rightIcon: String as PropType<ButtonProps['rightIcon']>,
2133
...vueThemingProps,
2234

2335
/** Not sure if the SystemCSSProperties is the right prop type for this */
2436
iconSpacing: {
25-
type: [String, Number, Array] as PropType<
26-
SystemCSSProperties['marginRight']
27-
>,
37+
type: [String, Number, Array] as PropType<ButtonProps['iconSpacing']>,
2838
default: '0.5rem',
2939
},
3040
}

packages/c-button/src/icon-button.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { h, ComponentObjectPropsOptions, defineComponent, PropType } from 'vue'
22
import CButton from './button'
3-
import { BUTTON_PROPS } from './button.utils'
3+
import { BUTTON_PROPS, ButtonProps } from './button.utils'
44
import { CIcon } from '@chakra-ui/c-icon'
5+
import { ComponentWithProps, DeepPartial } from '@chakra-ui/vue-system'
56

67
const IconButtonProps: ComponentObjectPropsOptions = {
78
...BUTTON_PROPS,
@@ -13,12 +14,20 @@ const IconButtonProps: ComponentObjectPropsOptions = {
1314
},
1415
}
1516

17+
export interface CIconButtonProps extends ButtonProps {
18+
icon: string
19+
isRound?: boolean
20+
ariaLabel: string
21+
}
22+
1623
/**
1724
* CIconButton
1825
*
1926
* IconButton composes the Button component except that it renders only an icon.
2027
*/
21-
const CIconButton = defineComponent({
28+
const CIconButton: ComponentWithProps<
29+
DeepPartial<CIconButtonProps>
30+
> = defineComponent({
2231
name: 'CIconButton',
2332
props: IconButtonProps,
2433
setup(props, { attrs }) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ColorMode } from '@chakra-ui/c-color-mode'
2+
import { ThemeOverride } from '../extend-theme'
3+
4+
interface ExtendIconsPath {
5+
path: string
6+
viewBox?: string
7+
}
8+
9+
interface IconsOptions {
10+
pack?: 'fa' | 'fe'
11+
library?: {}
12+
extend?: Record<string, ExtendIconsPath>
13+
}
14+
export interface ChakraPluginOptions {
15+
cssReset?: boolean
16+
extendTheme?: ThemeOverride
17+
icons?: IconsOptions
18+
defaultColorMode?: ColorMode
19+
}

packages/core/src/index.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@ import internalIcons from './icon.internals'
66
import { extendTheme, ThemeOverride } from './extend-theme'
77
import { MergedIcons, parseIcons } from './parse-icons'
88
import { mode } from '@chakra-ui/vue-theme-tools'
9+
import { ChakraPluginOptions } from './helpers/plugin.types'
910

10-
interface ExtendIconsPath {
11-
path: string
12-
viewBox?: string
13-
}
14-
15-
interface IconsOptions {
16-
pack?: 'fa' | 'fe'
17-
library?: {}
18-
extend?: Record<string, ExtendIconsPath>
19-
}
20-
export interface ChakraUIVuePluginOptions {
21-
cssReset?: boolean
22-
extendTheme?: ThemeOverride
23-
icons?: IconsOptions
24-
defaultColorMode?: ColorMode
11+
/**
12+
* Helper function to extend Chakra plugin with options
13+
* It just returns its arguments
14+
*/
15+
export function extendChakra(
16+
options: ChakraPluginOptions = { cssReset: true }
17+
) {
18+
return options
2519
}
2620

2721
const ChakraUIVuePlugin: Plugin = {
28-
install(app, options: ChakraUIVuePluginOptions = { cssReset: true }) {
22+
install(app, options: ChakraPluginOptions = { cssReset: true }) {
2923
// 1. Get theme value
3024
// 2. Parse theme tokens to CSS variables
3125
// 3. Inject all CSS variables as theme object
@@ -68,6 +62,7 @@ const ChakraUIVuePlugin: Plugin = {
6862
},
6963
}
7064

65+
export type { ChakraPluginOptions }
7166
export interface ThemeProviderProps extends ThemeOverride {}
7267
export default ChakraUIVuePlugin
7368
export { extendTheme }

packages/utils/src/props.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ThemingProps } from '@chakra-ui/vue-system'
1+
import { DOMElements, ThemingProps } from '@chakra-ui/vue-system'
22
import { PropType } from 'vue'
33

44
export const vueThemingProps = {
@@ -11,3 +11,11 @@ export const vueThemingProps = {
1111
export const SNA = [Number, String, Array]
1212
export const SAO = [String, Array, Object]
1313
export const SNAO = [Number, String, Array, Object]
14+
15+
export interface BaseThemedComponentProps
16+
extends Pick<
17+
ThemingProps,
18+
'colorScheme' | 'variant' | 'size' | 'styleConfig'
19+
> {
20+
as: DOMElements | 'router-link' | 'nuxt-link'
21+
}

website/src/docs-theme/components/MdxComponents.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ import { trim } from 'lodash'
1616
const LinkedHeading = (as: string, apply: string) => (
1717
props: any,
1818
context: SetupContext
19-
) =>
20-
h('MdxHeading', { ...props, as, apply }, renderSlot(context.slots, 'default'))
19+
) => {
20+
console.log(' RENDERING HEADING', props, context)
21+
return h(
22+
'MdxHeading',
23+
{ ...props, as, apply },
24+
renderSlot(context.slots, 'default')
25+
)
26+
}
2127

2228
const MdxChakra = (
2329
{
@@ -37,12 +43,14 @@ const MdxChakra = (
3743
)
3844

3945
export const MdxComponents = {
40-
h1: (props: any, context: SetupContext) =>
41-
h(
46+
h1: (props: any, context: SetupContext) => {
47+
console.log(' RENDERING HEADER', props, context)
48+
return h(
4249
'chakra.h1',
4350
{ apply: 'mdx.h1', ...props },
4451
renderSlot(context.slots, 'default')
45-
),
52+
)
53+
},
4654
h2: LinkedHeading('h2', 'mdx.h2'),
4755
h3: LinkedHeading('h3', 'mdx.h3'),
4856
h4: LinkedHeading('h4', 'mdx.h4'),

website/src/docs-theme/layout/LayoutMdx.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<template #sidebar>
44
<app-sidebar :routes="routes" />
55
</template>
6-
<template #default>
7-
<slot />
6+
<template>
7+
<slot name="default" />
88
</template>
99
<template #pagination>
1010
<app-pagination
@@ -46,6 +46,9 @@ export default defineComponent({
4646
removeFromLast(props.frontmatter.slug, '#'),
4747
routes
4848
)
49+
50+
console.log(slots?.default?.())
51+
4952
const routeContext = getRouteContext(route, routes)
5053
5154
return {

0 commit comments

Comments
 (0)