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

Commit f431efa

Browse files
committed
refactor(c-alert): use createContext helper
1 parent 97adef9 commit f431efa

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/c-alert/src/alert.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
DOMElements,
1111
} from '@chakra-ui/vue-system'
1212
import { SystemStyleObject } from '@chakra-ui/styled-system'
13+
import { createContext } from '@chakra-ui/vue-utils/src'
1314
import { CIcon } from '@chakra-ui/c-icon'
1415

1516
const STATUSES = {
@@ -34,10 +35,16 @@ const STATUSES = {
3435
type AlertStatus = keyof typeof STATUSES
3536
export type AlertVariant = 'solid' | 'subtle' | 'left-accent' | 'top-accent'
3637

37-
interface AlertState {
38+
interface AlertContext {
3839
status: AlertStatus
3940
}
4041

42+
const [AlertProvider, useAlertContext] = createContext<AlertContext>({
43+
name: 'AlertContext',
44+
errorMessage:
45+
'useAlertContext: `context` is undefined. Seems you forgot to wrap alert components in `<c-alert />`',
46+
})
47+
4148
/**
4249
* CAlert component
4350
*
@@ -85,7 +92,7 @@ export const CAlert = defineComponent({
8592
}
8693

8794
provideComponentStyles('Alert', styles.value)
88-
provide('$AlertState', { status: props.status } as AlertState)
95+
AlertProvider({ status: props.status })
8996

9097
return () =>
9198
h(
@@ -157,8 +164,8 @@ export const CAlertIcon = defineComponent({
157164
},
158165
},
159166
setup(props, { attrs }) {
160-
const alertState = inject<AlertState>('$AlertState')
161-
const { icon } = STATUSES[alertState?.status as AlertStatus]
167+
const { status } = useAlertContext()
168+
const { icon } = STATUSES[status]
162169
const styles = useComponentStyles('Alert')
163170

164171
const alertIcon = computed(() => props.icon || icon)

0 commit comments

Comments
 (0)