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(notifications): allow placement customizations for toast #1055

Merged
merged 3 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/notifications/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"index.cjs.js": {
"bundled": 34872,
"minified": 23552,
"gzipped": 5979
"bundled": 35167,
"minified": 23692,
"gzipped": 6006
},
"index.esm.js": {
"bundled": 32792,
"minified": 21697,
"gzipped": 5851,
"bundled": 33087,
"minified": 21837,
"gzipped": 5879,
"treeshaked": {
"rollup": {
"code": 11732,
"import_statements": 379
},
"webpack": {
"code": 19833
"code": 19932
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, renderRtl } from 'garden-test-utils';
import { useToast, ToastProvider } from '../../';
import { useToast, ToastProvider, IToastProviderProps } from '../../';

import { config } from 'react-transition-group';
config.disabled = true;

const ToastExample = () => {
const ToastExample: React.FC<IToastProviderProps> = props => {
const NotificationExample = () => {
const { addToast } = useToast();

Expand All @@ -27,7 +27,7 @@ const ToastExample = () => {
};

return (
<ToastProvider zIndex={100}>
<ToastProvider {...props}>
<NotificationExample />
</ToastProvider>
);
Expand Down Expand Up @@ -75,12 +75,33 @@ describe('ToastProvider', () => {
});

it('renders toasts with provided zIndex', () => {
const { getByRole, getByText } = render(<ToastExample />);
const { getByRole, getByText } = render(<ToastExample zIndex={100} />);

userEvent.click(getByRole('button'));
const notificationElement = getByText('notification');

expect(notificationElement).toBeInTheDocument();
expect(notificationElement.parentElement?.parentElement).toHaveStyleRule('z-index', '100');
});

it('applies placementProps when provided', () => {
const customizationProps = { 'data-test-id': 'placement-prop' } as any;

const { getAllByTestId } = render(
<ToastExample
placementProps={{
'top-start': customizationProps,
top: customizationProps,
'top-end': customizationProps,
'bottom-start': customizationProps,
bottom: customizationProps,
'bottom-end': customizationProps
}}
/>
);

const customizedPlacements = getAllByTestId('placement-prop');

expect(customizedPlacements).toHaveLength(6);
});
});
31 changes: 27 additions & 4 deletions packages/notifications/src/elements/toaster/ToastProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { createContext, useReducer, Dispatch, useCallback, useMemo } from 'react';
import React, {
createContext,
useReducer,
Dispatch,
useCallback,
useMemo,
HTMLAttributes
} from 'react';
import PropTypes from 'prop-types';

import {
Expand All @@ -30,9 +37,19 @@ export interface IToastProviderProps {
* Sets the `z-index` of the toast
*/
zIndex?: number;
/**
* Props to be spread onto the wrapping element for each Toast placement.
* Can be used to customize the positioning of a placement.
austingreendev marked this conversation as resolved.
Show resolved Hide resolved
*/
placementProps?: Partial<Record<ToastPlacement, HTMLAttributes<HTMLDivElement>>>;
}

export const ToastProvider: React.FC<IToastProviderProps> = ({ limit, zIndex, children }) => {
export const ToastProvider: React.FC<IToastProviderProps> = ({
limit,
zIndex,
placementProps = {},
children
}) => {
const [state, dispatch] = useReducer(toasterReducer, getInitialState());

const contextValue = useMemo(() => ({ state, dispatch }), [state, dispatch]);
Expand All @@ -46,10 +63,16 @@ export const ToastProvider: React.FC<IToastProviderProps> = ({ limit, zIndex, ch
}

return (
<ToastSlot placement={placement} toasts={matchingToasts} zIndex={zIndex} limit={limit!} />
<ToastSlot
placement={placement}
toasts={matchingToasts}
zIndex={zIndex}
limit={limit!}
{...placementProps[placement]}
/>
);
},
[limit, state.toasts, zIndex]
[limit, state.toasts, zIndex, placementProps]
);

return (
Expand Down
13 changes: 10 additions & 3 deletions packages/notifications/src/elements/toaster/ToastSlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { useCallback, useContext, useEffect, useState } from 'react';
import React, { HTMLAttributes, useCallback, useContext, useEffect, useState } from 'react';
import { ThemeContext } from 'styled-components';
import { CSSTransition } from 'react-transition-group';
import { useDocument } from '@zendeskgarden/react-theming';
import { IToast, ToastPlacement } from './reducer';
import { Toast } from './Toast';
import { StyledFadeInTransition, StyledTransitionGroup, TRANSITION_CLASS } from './styled';

interface IToastSlotProps {
interface IToastSlotProps extends HTMLAttributes<HTMLDivElement> {
toasts: IToast[];
placement: ToastPlacement;
limit: number;
zIndex?: number;
}

export const ToastSlot: React.FC<IToastSlotProps> = ({ toasts, placement, zIndex, limit }) => {
export const ToastSlot: React.FC<IToastSlotProps> = ({
toasts,
placement,
zIndex,
limit,
...props
}) => {
const [pauseTimers, setPauseTimers] = useState(false);
const theme = useContext(ThemeContext);
const environment = useDocument(theme);
Expand Down Expand Up @@ -71,6 +77,7 @@ export const ToastSlot: React.FC<IToastSlotProps> = ({ toasts, placement, zIndex
$zIndex={zIndex}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
{...props}
>
{toasts.map((toast, index) => {
const transitionRef = React.createRef<HTMLDivElement>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ describe('useToast()', () => {

expect(queryByRole('alert')).toBeInTheDocument();

jest.runOnlyPendingTimers();
act(() => {
jest.runOnlyPendingTimers();
});

expect(queryByRole('alert')).not.toBeInTheDocument();
});
Expand All @@ -146,7 +148,9 @@ describe('useToast()', () => {

expect(queryByRole('alert')).toBeInTheDocument();

jest.runOnlyPendingTimers();
act(() => {
jest.runOnlyPendingTimers();
});

expect(queryByRole('alert')).toBeInTheDocument();
});
Expand Down