From c3b7e2d65b3f9be07cd993d417bd1435f6abed5c Mon Sep 17 00:00:00 2001 From: Sophie Schneider Date: Thu, 10 Aug 2023 16:21:36 +0000 Subject: [PATCH] Merge BannerExperimental with Banner --- .../src/components/Banner/Banner.scss | 43 +++ .../src/components/Banner/Banner.stories.tsx | 13 + .../src/components/Banner/Banner.tsx | 292 +++++++++++++++--- .../BannerExperimental.scss | 41 --- .../BannerExperimental/BannerExperimental.tsx | 267 ---------------- .../components/BannerExperimental/index.ts | 1 - .../src/components/Banner/components/index.ts | 1 - polaris-react/src/components/Banner/index.ts | 1 + .../components/Banner/tests/Banner.test.tsx | 10 +- .../BannerExperimental => }/utilities.ts | 46 ++- 10 files changed, 361 insertions(+), 354 deletions(-) delete mode 100644 polaris-react/src/components/Banner/components/BannerExperimental/BannerExperimental.scss delete mode 100644 polaris-react/src/components/Banner/components/BannerExperimental/BannerExperimental.tsx delete mode 100644 polaris-react/src/components/Banner/components/BannerExperimental/index.ts delete mode 100644 polaris-react/src/components/Banner/components/index.ts rename polaris-react/src/components/Banner/{components/BannerExperimental => }/utilities.ts (64%) diff --git a/polaris-react/src/components/Banner/Banner.scss b/polaris-react/src/components/Banner/Banner.scss index 81c7d7de916..8d07c49126b 100644 --- a/polaris-react/src/components/Banner/Banner.scss +++ b/polaris-react/src/components/Banner/Banner.scss @@ -46,3 +46,46 @@ margin-top: var(--p-space-4); } } + +// stylelint-disable -- duplicate selectors to bump specificity for button svg color override +// https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity#increasing_specificity_by_duplicating_selector + +@mixin recolor-icon($fill-color: null) { + svg, + path { + fill: $fill-color; + } +} + +.icon-on-color.icon-on-color.icon-on-color { + @include recolor-icon(var(--p-color-icon-on-color)); +} + +.icon-success-strong-experimental.icon-success-strong-experimental.icon-success-strong-experimental { + @include recolor-icon(var(--p-color-icon-success-strong-experimental)); +} + +.text-warning-strong.text-warning-strong.text-warning-strong { + @include recolor-icon(var(--p-color-text-warning-strong)); +} + +.icon-warning-strong-experimental.icon-warning-strong-experimental.icon-warning-strong-experimental { + @include recolor-icon(var(--p-color-icon-warning-strong-experimental)); +} + +.icon-critical-strong-experimental.icon-critical-strong-experimental.icon-critical-strong-experimental { + @include recolor-icon(var(--p-color-icon-critical-strong-experimental)); +} + +.text-info-strong.text-info-strong.text-info-strong { + @include recolor-icon(var(--p-color-text-info-strong)); +} + +.icon-info-strong-experimental.icon-info-strong-experimental.icon-info-strong-experimental { + @include recolor-icon(var(--p-color-icon-info-strong-experimental)); +} + +.icon-subdued.icon-subdued.icon-subdued { + @include recolor-icon(var(--p-color-icon-subdued)); +} +// stylelint-enable diff --git a/polaris-react/src/components/Banner/Banner.stories.tsx b/polaris-react/src/components/Banner/Banner.stories.tsx index 2c4464a8e34..c36da8c4ee4 100644 --- a/polaris-react/src/components/Banner/Banner.stories.tsx +++ b/polaris-react/src/components/Banner/Banner.stories.tsx @@ -295,6 +295,19 @@ export function All() { } /> + + No title with hidden icon + + + Changing the phone number for this customer will unsubscribe them + from SMS marketing text messages until they provide consent. + + } + /> Only title diff --git a/polaris-react/src/components/Banner/Banner.tsx b/polaris-react/src/components/Banner/Banner.tsx index baeb758776c..f45459eff44 100644 --- a/polaris-react/src/components/Banner/Banner.tsx +++ b/polaris-react/src/components/Banner/Banner.tsx @@ -1,19 +1,36 @@ import React, { forwardRef, useContext, - useImperativeHandle, useRef, useState, + useEffect, + useCallback, } from 'react'; +import type {PropsWithChildren} from 'react'; +import type {ColorTextAlias} from '@shopify/polaris-tokens'; +import {CancelMinor} from '@shopify/polaris-icons'; -import {classNames} from '../../utilities/css'; -import {BannerContext} from '../../utilities/banner-context'; import type {Action, DisableableAction, LoadableAction} from '../../types'; +import {Text} from '../Text'; +import {VerticalStack} from '../VerticalStack'; +import type {HorizontalStackProps} from '../HorizontalStack'; +import {HorizontalStack} from '../HorizontalStack'; +import type {BoxProps} from '../Box'; +import {Box} from '../Box'; +import {Button} from '../Button'; +import {ButtonGroup} from '../ButtonGroup'; +import {Icon} from '../Icon'; import type {IconProps} from '../Icon'; +import {BannerContext} from '../../utilities/banner-context'; import {WithinContentContext} from '../../utilities/within-content-context'; +import {classNames} from '../../utilities/css'; +import {useBreakpoints} from '../../utilities/breakpoints'; +import {useI18n} from '../../utilities/i18n'; +import {useEventListener} from '../../utilities/use-event-listener'; import styles from './Banner.scss'; -import {BannerExperimental} from './components'; +import type {BannerHandles} from './utilities'; +import {bannerAttributes, useBannerFocus} from './utilities'; export type BannerStatus = 'success' | 'info' | 'warning' | 'critical'; @@ -67,48 +84,249 @@ export const Banner = forwardRef(function Banner( onKeyUp={handleKeyUp} onBlur={handleBlur} > - + ); }); -export interface BannerHandles { - focus(): void; +interface BannerLayoutProps { + backgroundColor: BoxProps['background']; + textColor: ColorTextAlias; + bannerTitle: React.ReactNode; + bannerIcon: React.ReactNode; + actionButtons: React.ReactNode; + dismissButton: React.ReactNode; } -function useBannerFocus(bannerRef: React.Ref) { - const wrapperRef = useRef(null); - const [shouldShowFocus, setShouldShowFocus] = useState(false); - - useImperativeHandle( - bannerRef, - () => ({ - focus: () => { - wrapperRef.current?.focus(); - setShouldShowFocus(true); - }, - }), - [], - ); +export function BannerLayout({ + status = 'info', + icon, + hideIcon, + onDismiss, + action, + secondaryAction, + title, + children, +}: BannerProps) { + const i18n = useI18n(); + const withinContentContainer = useContext(WithinContentContext); + const isInlineIconBanner = !title && !withinContentContainer; + const bannerStatus = Object.keys(bannerAttributes).includes(status) + ? status + : 'info'; + const bannerColors = + bannerAttributes[bannerStatus][ + withinContentContainer ? 'withinContentContainer' : 'withinPage' + ]; - const handleKeyUp = (event: React.KeyboardEvent) => { - if (event.target === wrapperRef.current) { - setShouldShowFocus(true); - } + const sharedBannerProps: BannerLayoutProps = { + backgroundColor: bannerColors.background, + textColor: bannerColors.text, + bannerTitle: title ? ( + + {title} + + ) : null, + bannerIcon: hideIcon ? null : ( + + + + ), + actionButtons: + action || secondaryAction ? ( + + {action && ( + + )} + {secondaryAction && ( + + )} + + ) : null, + dismissButton: onDismiss ? ( + - )} - {secondaryAction && ( - - )} - - ) : null, - dismissButton: onDismiss ? ( -