@sikka/hawa 0.1.29 → 0.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sikka/hawa",
3
- "version": "0.1.29",
3
+ "version": "0.1.31",
4
4
  "description": "SaaS Oriented UI Kit",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.es.js",
@@ -111,6 +111,7 @@
111
111
  "@radix-ui/react-popover": "^1.0.6",
112
112
  "@radix-ui/react-separator": "^1.0.3",
113
113
  "@radix-ui/react-tabs": "^1.0.4",
114
+ "@radix-ui/react-toast": "^1.1.4",
114
115
  "@radix-ui/react-tooltip": "^1.0.6",
115
116
  "class-variance-authority": "^0.7.0",
116
117
  "clsx": "^1.2.1",
@@ -1,4 +1,4 @@
1
- import React, { useState, FC } from "react"
1
+ import React, { useState, FC, useRef, useEffect } from "react"
2
2
  import clsx from "clsx"
3
3
 
4
4
  type RadioTypes = {
@@ -34,10 +34,22 @@ export const HawaRadio: FC<RadioTypes> = ({
34
34
  default: "max-w-fit",
35
35
  full: "w-full",
36
36
  }
37
+ const [parentDirection, setParentDirection] = useState(null)
38
+ const ref = useRef(null)
39
+
40
+ useEffect(() => {
41
+ if (ref.current && ref.current.parentNode) {
42
+ const dir = window.getComputedStyle(ref.current.parentNode).direction
43
+ setParentDirection(dir)
44
+ }
45
+ console.log("how many times")
46
+ })
47
+
37
48
  switch (design) {
38
49
  case "tabs":
39
50
  return (
40
51
  <ul
52
+ ref={ref}
41
53
  className={clsx(
42
54
  props.options?.length > 2
43
55
  ? "flex-wrap xs:max-w-full xs:flex-nowrap"
@@ -57,7 +69,11 @@ export const HawaRadio: FC<RadioTypes> = ({
57
69
  className={clsx(
58
70
  "w-full cursor-pointer",
59
71
  orientation === "horizontal" &&
72
+ parentDirection === "ltr" &&
60
73
  "rounded-none first:rounded-l last:rounded-r",
74
+ orientation === "horizontal" &&
75
+ parentDirection === "rtl" &&
76
+ "rounded-none first:rounded-r last:rounded-l",
61
77
  orientation === "vertical" &&
62
78
  "rounded-none first:rounded-t last:rounded-b",
63
79
 
@@ -0,0 +1,161 @@
1
+ import * as React from "react"
2
+ import * as ToastPrimitives from "@radix-ui/react-toast"
3
+ import { cva, type VariantProps } from "class-variance-authority"
4
+
5
+ import { cn } from "../util"
6
+
7
+ const ToastProvider = ToastPrimitives.Provider
8
+
9
+ const ToastViewport = React.forwardRef<
10
+ React.ElementRef<typeof ToastPrimitives.Viewport>,
11
+ React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
12
+ >(({ className, ...props }, ref) => (
13
+ <ToastPrimitives.Viewport
14
+ ref={ref}
15
+ className={cn(
16
+ "fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]",
17
+ className
18
+ )}
19
+ {...props}
20
+ />
21
+ ))
22
+ ToastViewport.displayName = ToastPrimitives.Viewport.displayName
23
+
24
+ const toastVariants = cva(
25
+ "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full",
26
+ {
27
+ variants: {
28
+ variant: {
29
+ default: "border bg-background text-foreground",
30
+ destructive:
31
+ "destructive group border-destructive bg-destructive text-destructive-foreground",
32
+ },
33
+ severity: {
34
+ info: "info group text-info-foreground bg-info",
35
+ warning: "warning group text-warning-foreground bg-warning",
36
+ error: "error group border-error bg-error text-error-foreground",
37
+ success: "success group text-success-foreground bg-success",
38
+ none: "bg-red-500 ",
39
+ },
40
+ },
41
+
42
+ defaultVariants: {
43
+ variant: "default",
44
+ },
45
+ }
46
+ )
47
+
48
+ const Toast = React.forwardRef<
49
+ React.ElementRef<typeof ToastPrimitives.Root>,
50
+ React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
51
+ VariantProps<typeof toastVariants> & {
52
+ severity?: "info" | "warning" | "error" | "success" | "none"
53
+ }
54
+ >(({ className, variant, severity = "none", ...props }, ref) => {
55
+ return (
56
+ <ToastPrimitives.Root
57
+ ref={ref}
58
+ className={cn(
59
+ toastVariants({ variant, severity }),
60
+ className
61
+ // severity === "error" && "bg-red-500"
62
+ )}
63
+ {...props}
64
+ />
65
+ )
66
+ })
67
+ Toast.displayName = ToastPrimitives.Root.displayName
68
+
69
+ const ToastAction = React.forwardRef<
70
+ React.ElementRef<typeof ToastPrimitives.Action>,
71
+ React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
72
+ >(({ className, ...props }, ref) => (
73
+ <ToastPrimitives.Action
74
+ ref={ref}
75
+ className={cn(
76
+ "inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
77
+ // "group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive",
78
+
79
+ "group-[.info]:border-muted/40 group-[.info]:hover:border-info/30 group-[.info]:hover:bg-info group-[.info]:hover:text-info-foreground group-[.info]:focus:ring-info",
80
+
81
+ "group-[.error]:border-muted/40 group-[.error]:hover:border-error/30 group-[.error]:hover:bg-error group-[.error]:hover:text-error-foreground group-[.error]:focus:ring-error",
82
+
83
+ "group-[.success]:border-muted/40 group-[.success]:hover:border-success/30 group-[.success]:hover:bg-success group-[.success]:hover:text-success-foreground group-[.success]:focus:ring-success",
84
+
85
+ "group-[.warning]:border-muted/40 group-[.warning]:hover:border-warning/30 group-[.warning]:hover:bg-warning group-[.warning]:hover:text-warning-foreground group-[.warning]:focus:ring-warning",
86
+ className
87
+ )}
88
+ {...props}
89
+ />
90
+ ))
91
+ ToastAction.displayName = ToastPrimitives.Action.displayName
92
+
93
+ const ToastClose = React.forwardRef<
94
+ React.ElementRef<typeof ToastPrimitives.Close>,
95
+ React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
96
+ >(({ className, ...props }, ref) => (
97
+ <ToastPrimitives.Close
98
+ ref={ref}
99
+ className={cn(
100
+ "absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
101
+ className
102
+ )}
103
+ toast-close=""
104
+ {...props}
105
+ >
106
+ <svg
107
+ aria-label="Close Icon"
108
+ aria-hidden="true"
109
+ className="h-4 w-4"
110
+ fill="currentColor"
111
+ viewBox="0 0 20 20"
112
+ >
113
+ <path
114
+ fillRule="evenodd"
115
+ d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
116
+ clipRule="evenodd"
117
+ ></path>
118
+ </svg>
119
+ </ToastPrimitives.Close>
120
+ ))
121
+ ToastClose.displayName = ToastPrimitives.Close.displayName
122
+
123
+ const ToastTitle = React.forwardRef<
124
+ React.ElementRef<typeof ToastPrimitives.Title>,
125
+ React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
126
+ >(({ className, ...props }, ref) => (
127
+ <ToastPrimitives.Title
128
+ ref={ref}
129
+ className={cn("text-sm font-semibold", className)}
130
+ {...props}
131
+ />
132
+ ))
133
+ ToastTitle.displayName = ToastPrimitives.Title.displayName
134
+
135
+ const ToastDescription = React.forwardRef<
136
+ React.ElementRef<typeof ToastPrimitives.Description>,
137
+ React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
138
+ >(({ className, ...props }, ref) => (
139
+ <ToastPrimitives.Description
140
+ ref={ref}
141
+ className={cn("text-sm opacity-90", className)}
142
+ {...props}
143
+ />
144
+ ))
145
+ ToastDescription.displayName = ToastPrimitives.Description.displayName
146
+
147
+ type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
148
+
149
+ type ToastActionElement = React.ReactElement<typeof ToastAction>
150
+
151
+ export {
152
+ type ToastProps,
153
+ type ToastActionElement,
154
+ ToastProvider,
155
+ ToastViewport,
156
+ Toast,
157
+ ToastTitle,
158
+ ToastDescription,
159
+ ToastClose,
160
+ ToastAction,
161
+ }
@@ -0,0 +1,33 @@
1
+ import {
2
+ Toast,
3
+ ToastClose,
4
+ ToastDescription,
5
+ ToastProvider,
6
+ ToastTitle,
7
+ ToastViewport,
8
+ } from './Toast'
9
+ import { useToast } from '../hooks/useToast'
10
+
11
+ export function Toaster() {
12
+ const { toasts } = useToast()
13
+
14
+ return (
15
+ <ToastProvider >
16
+ {toasts.map(function ({ id, title, description, action, ...props }) {
17
+ return (
18
+ <Toast key={id} {...props}>
19
+ <div className="grid gap-1">
20
+ {title && <ToastTitle>{title}</ToastTitle>}
21
+ {description && (
22
+ <ToastDescription>{description}</ToastDescription>
23
+ )}
24
+ </div>
25
+ {action}
26
+ <ToastClose />
27
+ </Toast>
28
+ )
29
+ })}
30
+ <ToastViewport className='gap-2'/>
31
+ </ToastProvider>
32
+ )
33
+ }
@@ -67,3 +67,5 @@ export * from "./Popover"
67
67
  export * from "./Tabs"
68
68
  export * from "./Textarea"
69
69
  export * from "./Separator"
70
+ export * from "./Toast"
71
+ export * from "./Toaster"
@@ -1,2 +1,3 @@
1
1
  export * from "./useDiscloser"
2
2
  export * from "./useHover"
3
+ export * from "./useToast"
@@ -0,0 +1,190 @@
1
+ // Inspired by react-hot-toast library
2
+ import * as React from "react"
3
+
4
+ import type { ToastActionElement, ToastProps } from "../elements/Toast"
5
+
6
+ const TOAST_LIMIT = 5
7
+ const TOAST_REMOVE_DELAY = 1000000
8
+
9
+ type ToasterToast = ToastProps & {
10
+ id: string
11
+ title?: React.ReactNode
12
+ description?: React.ReactNode
13
+ severity?: "info" | "warning" | "error" | "success" | "none"
14
+ action?: ToastActionElement
15
+ }
16
+
17
+ const actionTypes = {
18
+ ADD_TOAST: "ADD_TOAST",
19
+ UPDATE_TOAST: "UPDATE_TOAST",
20
+ DISMISS_TOAST: "DISMISS_TOAST",
21
+ REMOVE_TOAST: "REMOVE_TOAST",
22
+ } as const
23
+
24
+ let count = 0
25
+
26
+ function genId() {
27
+ count = (count + 1) % Number.MAX_VALUE
28
+ return count.toString()
29
+ }
30
+
31
+ type ActionType = typeof actionTypes
32
+
33
+ type Action =
34
+ | {
35
+ type: ActionType["ADD_TOAST"]
36
+ toast: ToasterToast
37
+ }
38
+ | {
39
+ type: ActionType["UPDATE_TOAST"]
40
+ toast: Partial<ToasterToast>
41
+ }
42
+ | {
43
+ type: ActionType["DISMISS_TOAST"]
44
+ toastId?: ToasterToast["id"]
45
+ }
46
+ | {
47
+ type: ActionType["REMOVE_TOAST"]
48
+ toastId?: ToasterToast["id"]
49
+ }
50
+
51
+ interface State {
52
+ toasts: ToasterToast[]
53
+ }
54
+
55
+ const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
56
+
57
+ const addToRemoveQueue = (toastId: string) => {
58
+ if (toastTimeouts.has(toastId)) {
59
+ return
60
+ }
61
+
62
+ const timeout = setTimeout(() => {
63
+ toastTimeouts.delete(toastId)
64
+ dispatch({
65
+ type: "REMOVE_TOAST",
66
+ toastId: toastId,
67
+ })
68
+ }, TOAST_REMOVE_DELAY)
69
+
70
+ toastTimeouts.set(toastId, timeout)
71
+ }
72
+
73
+ export const reducer = (state: State, action: Action): State => {
74
+ switch (action.type) {
75
+ case "ADD_TOAST":
76
+ return {
77
+ ...state,
78
+ toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
79
+ }
80
+
81
+ case "UPDATE_TOAST":
82
+ return {
83
+ ...state,
84
+ toasts: state.toasts.map((t) =>
85
+ t.id === action.toast.id ? { ...t, ...action.toast } : t
86
+ ),
87
+ }
88
+
89
+ case "DISMISS_TOAST": {
90
+ const { toastId } = action
91
+
92
+ // ! Side effects ! - This could be extracted into a dismissToast() action,
93
+ // but I'll keep it here for simplicity
94
+ if (toastId) {
95
+ addToRemoveQueue(toastId)
96
+ } else {
97
+ state.toasts.forEach((toast) => {
98
+ addToRemoveQueue(toast.id)
99
+ })
100
+ }
101
+
102
+ return {
103
+ ...state,
104
+ toasts: state.toasts.map((t) =>
105
+ t.id === toastId || toastId === undefined
106
+ ? {
107
+ ...t,
108
+ open: false,
109
+ }
110
+ : t
111
+ ),
112
+ }
113
+ }
114
+ case "REMOVE_TOAST":
115
+ if (action.toastId === undefined) {
116
+ return {
117
+ ...state,
118
+ toasts: [],
119
+ }
120
+ }
121
+ return {
122
+ ...state,
123
+ toasts: state.toasts.filter((t) => t.id !== action.toastId),
124
+ }
125
+ }
126
+ }
127
+
128
+ const listeners: Array<(state: State) => void> = []
129
+
130
+ let memoryState: State = { toasts: [] }
131
+
132
+ function dispatch(action: Action) {
133
+ memoryState = reducer(memoryState, action)
134
+ listeners.forEach((listener) => {
135
+ listener(memoryState)
136
+ })
137
+ }
138
+
139
+ type Toast = Omit<ToasterToast, "id">
140
+
141
+ function toast({ ...props }: Toast) {
142
+ const id = genId()
143
+
144
+ const update = (props: ToasterToast) =>
145
+ dispatch({
146
+ type: "UPDATE_TOAST",
147
+ toast: { ...props, id },
148
+ })
149
+ const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
150
+
151
+ dispatch({
152
+ type: "ADD_TOAST",
153
+ toast: {
154
+ ...props,
155
+ id,
156
+ open: true,
157
+ onOpenChange: (open) => {
158
+ if (!open) dismiss()
159
+ },
160
+ },
161
+ })
162
+
163
+ return {
164
+ id: id,
165
+ dismiss,
166
+ update,
167
+ }
168
+ }
169
+
170
+ function useToast() {
171
+ const [state, setState] = React.useState<State>(memoryState)
172
+
173
+ React.useEffect(() => {
174
+ listeners.push(setState)
175
+ return () => {
176
+ const index = listeners.indexOf(setState)
177
+ if (index > -1) {
178
+ listeners.splice(index, 1)
179
+ }
180
+ }
181
+ }, [state])
182
+
183
+ return {
184
+ ...state,
185
+ toast,
186
+ dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
187
+ }
188
+ }
189
+
190
+ export { useToast, toast }
@@ -215,7 +215,7 @@ export const AppLayout: React.FunctionComponent<AppLayoutTypes> = ({
215
215
  trigger={
216
216
  <div className="relative h-8 w-8 cursor-pointer overflow-clip rounded ring-1 ring-primary/30 dark:bg-gray-600">
217
217
  <svg
218
- aria-aria-label="Avatar Icon"
218
+ aria-label="Avatar Icon"
219
219
  className="absolute -left-1 h-10 w-10 text-gray-400"
220
220
  fill="currentColor"
221
221
  viewBox="0 0 20 20"