Skip to content

Commit

Permalink
fix(toast): resolve an issue with toast api
Browse files Browse the repository at this point in the history
  • Loading branch information
cschroeter committed Mar 4, 2024
1 parent 03ad778 commit a3592df
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/frameworks/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: All notable changes to this project will be documented in this file
### Fixed

- Resolved an issue with using `Locale` in Next.js projects.
- Resolved an issue with `Toast` not updating its toasts and count properties when creating one or more toasts.

## [2.2.2] - 2024-02-27

Expand Down
7 changes: 6 additions & 1 deletion packages/frameworks/react/src/toast/create-toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export const createToaster = (props: CreateToasterProps): CreateToasterReturn =>
const service = toast.group.machine({ id: '1', placement, ...rest }).start()
let api = toast.group.connect(service.getState(), service.send, normalizeProps)

const subscribe = (fn: CallableFunction) => {
// @ts-expect-error FIX LATER IT EXISTS
return service.subscribe((state) => fn(state.context.toasts))
}

const Toaster = forwardRef<HTMLOListElement, HTMLArkProps<'ol'>>((props, ref) => {
const getRootNode = useEnvironmentContext()
const [state, send] = useActor(service)
Expand All @@ -46,7 +51,7 @@ export const createToaster = (props: CreateToasterProps): CreateToasterReturn =>

Toaster.displayName = 'ToastGroup'

return [Toaster, api]
return [Toaster, Object.assign(api, { subscribe })]
}

interface ToastProviderFactoryProps {
Expand Down
15 changes: 12 additions & 3 deletions packages/frameworks/react/src/toast/stories/toast.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Meta } from '@storybook/react'
import { useEffect } from 'react'
import { Toast, createToaster } from '../'
import './toast.css'

Expand All @@ -21,11 +22,19 @@ export const Basic = () => {
},
})

useEffect(
() => toast.subscribe((toasts) => toasts.forEach((toast) => console.log('Toast:', toast))),
[toast],
)

const handleToast = () => {
const id = toast.create({ title: 'Title', description: 'Description' })
console.log('Toast ID:', id)
}

return (
<>
<button onClick={() => toast.create({ title: 'Title', description: 'Description' })}>
Toast
</button>
<button onClick={handleToast}>Toast</button>
<Toaster />
</>
)
Expand Down
4 changes: 4 additions & 0 deletions packages/frameworks/solid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ description: All notable changes to this project will be documented in this file

## [Unreleased]

### Fixed

- Resolved an issue with `Toast` not updating its toasts and count properties when creating one or more toasts.

## [2.2.0] - 2024-02-27

### Added
Expand Down
7 changes: 6 additions & 1 deletion packages/frameworks/solid/src/toast/create-toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export const createToaster = (props: CreateToasterProps): CreateToasterReturn =>
const [state, send] = useActor(service)
const api = createMemo(() => toast.group.connect(state, send, normalizeProps))

const subscribe = (fn: CallableFunction) => {
// @ts-expect-error FIX LATER IT EXISTS
return service.subscribe((state) => fn(state.context.toasts))
}

const Toaster = (toasterProps: HTMLArkProps<'ol'>) => {
const getRootNode = useEnvironmentContext()

Expand All @@ -46,7 +51,7 @@ export const createToaster = (props: CreateToasterProps): CreateToasterReturn =>
)
}

return [Toaster, api]
return [Toaster, createMemo(() => Object.assign(api(), { subscribe }))]
}

interface ToastProviderFactoryProps {
Expand Down
14 changes: 11 additions & 3 deletions packages/frameworks/solid/src/toast/stories/toast.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createEffect } from 'solid-js'
import type { Meta } from 'storybook-solidjs'
import { Toast, createToaster } from '../'
import './toast.css'
Expand All @@ -22,11 +23,18 @@ export const Basic = () => {
},
})

createEffect(() =>
toast().subscribe((toasts) => toasts.forEach((toast) => console.log('Toast:', toast))),
)

const handleToast = () => {
const id = toast().create({ title: 'Title', description: 'Description' })
console.log('Toast ID:', id)
}

return (
<>
<button onClick={() => toast().create({ title: 'Title', description: 'Description' })}>
Toast
</button>
<button onClick={handleToast}>Toast</button>
<Toaster />
</>
)
Expand Down

0 comments on commit a3592df

Please sign in to comment.