Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow ReactNode as children/content of the Toast - closes #6038 #6114

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/@react-spectrum/toast/chromatic/Toast.chromatic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ export const LongContent = {
export const LongContentAction = {
args: {variant: 'positive', children: 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra.', actionLabel: 'Undo'}
};

export const WithReactNodeContent = {
args: {variant: 'positive', children: <React.Fragment><strong>Neutral</strong> <i>toast</i> <b>with</b> <em>ReactNode</em> content</React.Fragment>}
};

export const WithReactNodeContentAction = {
args: {variant: 'positive', children: <React.Fragment><strong>Neutral</strong> <i>toast</i> <b>with</b> <em>ReactNode</em> content</React.Fragment>, actionLabel: 'Undo'}
};
14 changes: 14 additions & 0 deletions packages/@react-spectrum/toast/docs/Toast.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ function Example() {
}
```

## ReactNode as content

Toasts can accept a `ReactNode` as content, allowing for more complex content, such as formatted text or other components. When using a `ReactNode`, be sure to provide an accessible description for screen readers.

```tsx example
<Button
///- begin highlight -///
onPress={() => ToastQueue.info(<div><b>Bold</b> and <i>italic</i> toast</div>)}
///- end highlight -///
variant="primary">
Show toast
</Button>
```

## API

### ToastQueue
Expand Down
2 changes: 1 addition & 1 deletion packages/@react-spectrum/toast/src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {useLocalizedStringFormatter} from '@react-aria/i18n';
import {useToast} from '@react-aria/toast';

export interface SpectrumToastValue {
children: string,
children: string | React.ReactNode,
variant: 'positive' | 'negative' | 'info' | 'neutral',
actionLabel?: string,
onAction?: () => void,
Expand Down
12 changes: 6 additions & 6 deletions packages/@react-spectrum/toast/src/ToastContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {AriaToastRegionProps} from '@react-aria/toast';
import type {AriaToastRegionProps} from '@react-aria/toast';
import React, {ReactElement, useEffect, useRef} from 'react';
import {SpectrumToastValue, Toast} from './Toast';
import {Toaster} from './Toaster';
Expand Down Expand Up @@ -119,7 +119,7 @@ export function ToastContainer(props: SpectrumToastContainerProps): ReactElement
return null;
}

function addToast(children: string, variant: SpectrumToastValue['variant'], options: SpectrumToastOptions = {}) {
function addToast(children: string | React.ReactNode, variant: SpectrumToastValue['variant'], options: SpectrumToastOptions = {}) {
// Dispatch a custom event so that toasts can be intercepted and re-targeted, e.g. when inside an iframe.
if (typeof CustomEvent !== 'undefined' && typeof window !== 'undefined') {
let event = new CustomEvent('react-spectrum-toast', {
Expand Down Expand Up @@ -157,19 +157,19 @@ function addToast(children: string, variant: SpectrumToastValue['variant'], opti

const SpectrumToastQueue = {
/** Queues a neutral toast. */
neutral(children: string, options: SpectrumToastOptions = {}): CloseFunction {
neutral(children: string | React.ReactNode, options: SpectrumToastOptions = {}): CloseFunction {
return addToast(children, 'neutral', options);
},
/** Queues a positive toast. */
positive(children: string, options: SpectrumToastOptions = {}): CloseFunction {
positive(children: string | React.ReactNode, options: SpectrumToastOptions = {}): CloseFunction {
return addToast(children, 'positive', options);
},
/** Queues a negative toast. */
negative(children: string, options: SpectrumToastOptions = {}): CloseFunction {
negative(children: string | React.ReactNode, options: SpectrumToastOptions = {}): CloseFunction {
return addToast(children, 'negative', options);
},
/** Queues an informational toast. */
info(children: string, options: SpectrumToastOptions = {}): CloseFunction {
info(children: string | React.ReactNode, options: SpectrumToastOptions = {}): CloseFunction {
return addToast(children, 'info', options);
}
};
Expand Down
43 changes: 43 additions & 0 deletions packages/@react-spectrum/toast/stories/Toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ Default.story = {
}
};

export const WithReactNode = (args) => <RenderProviderWithReactNode {...args} />;
Default.story = {
parameters: {
a11y: {
config: {
rules: [
{id: 'landmark-main-is-top-level', enabled: false},
{id: 'landmark-no-duplicate-main', enabled: false},
{id: 'landmark-unique', enabled: false}
]
}
}
}
};


export const WithAction = (args) => (
<RenderProvider {...args} actionLabel="Action" onAction={action('onAction')} />
Expand Down Expand Up @@ -192,6 +207,34 @@ function RenderProvider(options: SpectrumToastOptions) {
);
}

function RenderProviderWithReactNode(options: SpectrumToastOptions) {
return (
<ButtonGroup>
<Button
onPress={() => ToastQueue.neutral(<div>Toast <i>available</i></div>, {...options, onClose: action('onClose')})}
variant="secondary">
Show Neutral Toast
</Button>
<Button
onPress={() => ToastQueue.positive(<span>Toast is <b>done</b>!</span>, {...options, onClose: action('onClose')})}
variant="primary">
Show Positive Toast
</Button>
<Button
onPress={() => ToastQueue.negative(<div>Toast is <em>burned</em>!</div>, {...options, onClose: action('onClose')})}
variant="negative">
Show Negative Toast
</Button>
<Button
onPress={() => ToastQueue.info(<span><strong>Toasting</strong>…</span>, {...options, onClose: action('onClose')})}
variant="accent"
style="outline">
Show Info Toast
</Button>
</ButtonGroup>
);
}

function ToastToggle(options: SpectrumToastOptions) {
let [close, setClose] = useState(null);

Expand Down
24 changes: 23 additions & 1 deletion packages/@react-spectrum/toast/test/ToastContainer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function RenderToastButton(props = {}) {
return (
<div>
<Button
onPress={() => ToastQueue[props.variant || 'neutral']('Toast is default', props)}
onPress={() => ToastQueue[props.variant || 'neutral'](props.content ?? 'Toast is default', props)}
variant="primary">
Show Default Toast
</Button>
Expand Down Expand Up @@ -372,4 +372,26 @@ describe('Toast Provider and Container', function () {
let region = getByRole('region');
expect(region).toHaveAttribute('aria-label', 'Toasts');
});

it('should support ReactNode as children/content', () => {
let {getByRole} = renderComponent(
<RenderToastButton
content={
<React.Fragment>
<strong>My new folder</strong> created within <b>Week 1</b>
</React.Fragment>
} />
);

const button = getByRole('button');
triggerPress(button);

const alert = getByRole('alert');
expect(alert).toBeVisible();
expect(alert).toHaveTextContent('My new folder created within Week 1');
expect(alert.querySelector('strong')).toBeVisible();
expect(alert.querySelector('strong')).toHaveTextContent('My new folder');
expect(alert.querySelector('b')).toBeVisible();
expect(alert.querySelector('b')).toHaveTextContent('Week 1');
});
});