-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs TS-1939
- Loading branch information
Showing
9 changed files
with
219 additions
and
171 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
apps/cyberstorm-storybook/stories/components/Toast.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { StoryFn, Meta } from "@storybook/react"; | ||
import { Toast, ToastProps } from "@thunderstore/cyberstorm"; | ||
import React from "react"; | ||
import { v4 as uuid } from "uuid"; | ||
|
||
const meta = { | ||
title: "Cyberstorm/Components/Toast", | ||
component: Toast, | ||
} as Meta<typeof Toast>; | ||
|
||
const defaultArgs: ToastProps = { | ||
id: uuid(), | ||
message: "-", | ||
variant: "info", | ||
}; | ||
|
||
const Template: StoryFn<typeof Toast> = (args) => ( | ||
<div style={{ width: "500px" }}> | ||
<Toast {...args} /> | ||
</div> | ||
); | ||
|
||
const MinimalToast = Template.bind({}); | ||
MinimalToast.args = { | ||
...defaultArgs, | ||
}; | ||
|
||
const InfoToast = Template.bind({}); | ||
InfoToast.args = { | ||
...defaultArgs, | ||
content: | ||
"Lorem ipsum dolor sit amet, lollero pollero long ass text right here ellipsis just kidding it’s not that long.", | ||
variant: "info", | ||
}; | ||
|
||
const DangerToast = Template.bind({}); | ||
DangerToast.args = { | ||
...defaultArgs, | ||
content: | ||
"Lorem ipsum dolor sit amet, lollero pollero long ass text right here ellipsis just kidding it’s not that long.", | ||
variant: "danger", | ||
}; | ||
|
||
const WarningToast = Template.bind({}); | ||
WarningToast.args = { | ||
...defaultArgs, | ||
content: | ||
"Lorem ipsum dolor sit amet, lollero pollero long ass text right here ellipsis just kidding it’s not that long.", | ||
variant: "warning", | ||
}; | ||
|
||
const SuccessToast = Template.bind({}); | ||
SuccessToast.args = { | ||
...defaultArgs, | ||
content: | ||
"Lorem ipsum dolor sit amet, lollero pollero long ass text right here ellipsis just kidding it’s not that long.", | ||
variant: "success", | ||
}; | ||
|
||
export { | ||
meta as default, | ||
MinimalToast, | ||
InfoToast, | ||
DangerToast, | ||
WarningToast, | ||
SuccessToast, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
"use client"; | ||
import * as RadixTooltip from "@radix-ui/react-tooltip"; | ||
import { ReactNode } from "react"; | ||
import { ToastProvider } from "./Toast/ToastContext"; | ||
import Toast from "./Toast"; | ||
|
||
interface CyberstormProvidersProps { | ||
children: ReactNode | ReactNode[]; | ||
tooltipDelay?: number; | ||
toastDuration?: number; | ||
} | ||
|
||
export function CyberstormProviders(props: CyberstormProvidersProps) { | ||
return ( | ||
<ToastProvider> | ||
<RadixTooltip.Provider delayDuration={80}> | ||
<Toast.Provider | ||
toastDuration={props.toastDuration ? props.toastDuration : 10000} | ||
> | ||
<RadixTooltip.Provider | ||
delayDuration={props.toastDuration ? props.toastDuration : 80} | ||
> | ||
{props.children} | ||
</RadixTooltip.Provider> | ||
</ToastProvider> | ||
</Toast.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { createContext, PropsWithChildren, useReducer } from "react"; | ||
import { v4 as uuid } from "uuid"; | ||
import * as RadixToast from "@radix-ui/react-toast"; | ||
import Toast from "."; | ||
import { ToastProps } from "./Toast"; | ||
|
||
const initState: { | ||
toasts: ToastProps[]; | ||
} = { toasts: [] }; | ||
|
||
const toastReducer = ( | ||
state: { | ||
toasts: ToastProps[]; | ||
}, | ||
action: { | ||
type: "add" | "delete"; | ||
toast: ToastProps; | ||
} | ||
) => { | ||
switch (action.type) { | ||
case "add": { | ||
return { | ||
toasts: [...state.toasts, action.toast], | ||
}; | ||
} | ||
case "delete": { | ||
const updatedToasts = state.toasts.filter( | ||
(toast) => toast.id !== action.toast.id | ||
); | ||
return { | ||
toasts: updatedToasts, | ||
}; | ||
} | ||
default: | ||
throw new Error(`Unhandled action: ${action.type}`); | ||
} | ||
}; | ||
|
||
interface ContextInterface { | ||
addToast: (props: Omit<ToastProps, "id">) => void; | ||
remove: (id: string) => void; | ||
} | ||
|
||
const ToastContext = createContext<ContextInterface | null>(null); | ||
|
||
export function Provider(props: { toastDuration: number } & PropsWithChildren) { | ||
const [state, dispatch] = useReducer(toastReducer, initState); | ||
|
||
const addToast = (props: Omit<ToastProps, "id">) => { | ||
const id = uuid(); | ||
dispatch({ type: "add", toast: { id, ...props } }); | ||
}; | ||
|
||
const remove = (id: string) => { | ||
dispatch({ type: "delete", toast: { id } }); | ||
}; | ||
|
||
const value = { | ||
addToast, | ||
remove, | ||
}; | ||
|
||
return ( | ||
<ToastContext.Provider value={value}> | ||
<RadixToast.Provider swipeDirection="left" duration={props.toastDuration}> | ||
{props.children} | ||
<Toast.Viewport toasts={state.toasts} /> | ||
</RadixToast.Provider> | ||
</ToastContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
packages/cyberstorm/src/components/Toast/ToastContainer.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.