File size: 3,286 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use client"

import { Fragment, forwardRef } from "react"
import { createContext } from "../../create-context"
import {
  type ConditionalValue,
  type HTMLChakraProps,
  type SlotRecipeProps,
  type UnstyledProp,
  chakra,
  createSlotRecipeContext,
} from "../../styled-system"
import { CheckCircleIcon, InfoIcon, WarningIcon } from "../icons"

interface StatusProps {
  status: ConditionalValue<"info" | "warning" | "success" | "error" | "neutral">
}

export const [AlertStatusProvider, useAlertStatusContext] =
  createContext<StatusProps>({
    name: "AlertStatusContext",
    hookName: "useAlertStatusContext",
    providerName: "<Alert />",
  })

////////////////////////////////////////////////////////////////////////////////////

const {
  withProvider,
  withContext,
  useStyles: useAlertStyles,
  PropsProvider,
} = createSlotRecipeContext({ key: "alert" })

export { useAlertStyles }

////////////////////////////////////////////////////////////////////////////////////

export interface AlertRootBaseProps
  extends SlotRecipeProps<"alert">,
    UnstyledProp {}

export interface AlertRootProps
  extends HTMLChakraProps<"div", AlertRootBaseProps> {}

export const AlertRoot = withProvider<HTMLDivElement, AlertRootProps>(
  "div",
  "root",
  {
    forwardAsChild: true,
    wrapElement(element, props) {
      return (
        // @ts-ignore fix later
        <AlertStatusProvider value={{ status: props.status || "info" }}>
          {element}
        </AlertStatusProvider>
      )
    },
  },
)

////////////////////////////////////////////////////////////////////////////////////

export const AlertPropsProvider =
  PropsProvider as React.Provider<AlertRootBaseProps>

////////////////////////////////////////////////////////////////////////////////////

export interface AlertTitleProps extends HTMLChakraProps<"div">, UnstyledProp {}

export const AlertTitle = withContext<HTMLDivElement, AlertTitleProps>(
  "div",
  "title",
)

////////////////////////////////////////////////////////////////////////////////////

export interface AlertDescriptionProps
  extends HTMLChakraProps<"div">,
    UnstyledProp {}

export const AlertDescription = withContext<
  HTMLDivElement,
  AlertDescriptionProps
>("div", "description")

////////////////////////////////////////////////////////////////////////////////////

export interface AlertContentProps
  extends HTMLChakraProps<"div">,
    UnstyledProp {}

export const AlertContent = withContext<HTMLDivElement, AlertContentProps>(
  "div",
  "content",
)

////////////////////////////////////////////////////////////////////////////////////

const iconMap = {
  info: InfoIcon,
  warning: WarningIcon,
  success: CheckCircleIcon,
  error: WarningIcon,
  neutral: InfoIcon,
}

export interface AlertIndicatorProps extends HTMLChakraProps<"span"> {}

export const AlertIndicator = forwardRef<SVGSVGElement, AlertIndicatorProps>(
  function AlertIndicator(props, ref) {
    const api = useAlertStatusContext()
    const styles = useAlertStyles()

    const Icon = typeof api.status === "string" ? iconMap[api.status] : Fragment
    const { children = <Icon />, ...rest } = props

    return (
      <chakra.span ref={ref} {...rest} css={[styles.indicator, props.css]}>
        {children}
      </chakra.span>
    )
  },
)