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: Introduce the USWDS Site Alert as info banner, use the USWDS Banner as intended #1328

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions app/scripts/components/common/banner/banner.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.usa-banner__button:after {
top: 3px;
}
170 changes: 114 additions & 56 deletions app/scripts/components/common/banner/index.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,136 @@
import React, { useState } from 'react';
import { Icon } from '@trussworks/react-uswds';
import { decode } from 'he';
import {
USWDSBanner,
USWDSBannerContent
USWDSBannerContent,
USWDSBannerButton,
USWDSBannerFlag,
USWDSBannerHeader,
USWDSBannerIcon,
USWDSBannerGuidance,
USWDSMediaBlockBody
} from '$components/common/uswds/banner';

const BANNER_KEY = 'dismissedBannerUrl';

function hasExpired(expiryDatetime) {
const expiryDate = new Date(expiryDatetime);
const currentDate = new Date();
return !!(currentDate > expiryDate);
interface Guidance {
left?: GuidanceContent;
right?: GuidanceContent;
}

enum BannerType {
info = 'info',
warning = 'warning'
interface GuidanceContent {
icon?: string;
iconAlt?: string;
title?: string;
text?: string;
}

const infoTypeFlag = BannerType.info;
interface BannerProps {
appTitle: string;
expires: Date;
url: string;
text: string;
type?: BannerType;
headerText?: string;
headerActionText?: string;
ariaLabel?: string;
flagImgAlt?: string;
leftGuidance?: GuidanceContent;
rightGuidance?: GuidanceContent;
className?: string;
defaultIsOpen?: boolean;
contentId?: string;
}

const DEFAULT_HEADER_TEXT =
'An official website of the United States government';

const DEFAULT_HEADER_ACTION_TEXT = "Here's how you know";

const DEFAULT_GUIDANCE: Guidance = {
left: {
title: 'Official websites use .gov',
text: 'A .gov website belongs to an official government organization in the United States.',
iconAlt: 'Dot gov icon',
icon: '/img/icon-dot-gov.svg'
},
right: {
title: 'Secure .gov websites use HTTPS',
text: `
A <strong>lock</strong> or <strong>https://</strong> means you've safely
connected to the .gov website. Share sensitive information only on
official, secure websites.
`,
iconAlt: 'HTTPS icon',
icon: '/img/icon-https.svg'
}
};

const GuidanceBlock = ({
content,
className
}: {
content: GuidanceContent;
className?: string;
}) => (
<USWDSBannerGuidance className={className}>
<USWDSBannerIcon src={content.icon} alt={content.iconAlt ?? ''} />
<USWDSMediaBlockBody>
<p>
<strong>{content.title}</strong>
<br />
<span
dangerouslySetInnerHTML={{ __html: decode(content.text ?? '') }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dzole0311 I am sorry I am late on this.
Both banner and site alert expects users to input their contents in markdown, and we compile it through parcel resovler: https://github.com/NASA-IMPACT/veda-ui/blob/main/parcel-resolver-veda/index.js#L89 I think we want the inputs to be coherent across the components - so maybe we can revisit this approach at some point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hanbyul-here! Does this mean we will restrict the Banner component to only allow users to specify a predefined set of content types (e.g., a url prop and a text prop) for which we would then render the appropriate html tags (<p />, <a />) directly within the Banner?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - I meant that user can pass the contents as markdown like ::markdown ### heading - I see that you used some html class for mock data https://github.com/NASA-IMPACT/veda-ui/blob/main/mock/veda.config.js#L133 I think since markdown accepts html as a part of markdown, it should be handled ok with markdown parser? I mainly want all the user-configured inputs for our component contents to be in a coherent format, and the parsing of the contents to be handled in the same place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a ticket for myself: #1348

/>
</p>
</USWDSMediaBlockBody>
</USWDSBannerGuidance>
);

export default function Banner({
appTitle,
expires,
url,
text,
type = infoTypeFlag
headerText,
headerActionText,
ariaLabel,
flagImgAlt = '',
leftGuidance,
rightGuidance,
className = '',
defaultIsOpen = false,
contentId = 'gov-banner-content'
}: BannerProps) {
const [isOpen, setIsOpen] = useState(defaultIsOpen);

const showBanner = localStorage.getItem(BANNER_KEY) !== url;
const [isOpen, setIsOpen] = useState(showBanner && !hasExpired(expires));
const leftContent = {
...DEFAULT_GUIDANCE.left,
...leftGuidance
} as GuidanceContent;

function onClose() {
localStorage.setItem(BANNER_KEY, url);
setIsOpen(false);
}
const rightContent = {
...DEFAULT_GUIDANCE.right,
...rightGuidance
} as GuidanceContent;

return (
<div>
{isOpen && (
<div className='position-relative'>
<USWDSBanner
aria-label={appTitle}
className={type !== infoTypeFlag ? 'bg-secondary-lighter' : ''}
>
<a href={url} target='_blank' rel='noreferrer'>
<USWDSBannerContent
className='padding-top-1 padding-bottom-1'
isOpen={true}
>
<div dangerouslySetInnerHTML={{ __html: text }} />
<USWDSBanner
aria-label={ariaLabel ?? DEFAULT_HEADER_TEXT}
className={className}
>
<USWDSBannerHeader
isOpen={isOpen}
flagImg={
<USWDSBannerFlag src='/img/us_flag_small.png' alt={flagImgAlt} />
}
headerText={headerText ?? DEFAULT_HEADER_TEXT}
headerActionText={headerActionText ?? DEFAULT_HEADER_ACTION_TEXT}
>
<USWDSBannerButton
isOpen={isOpen}
onClick={() => setIsOpen((prev) => !prev)}
aria-controls={contentId}
>
{headerActionText ?? DEFAULT_HEADER_ACTION_TEXT}
</USWDSBannerButton>
</USWDSBannerHeader>

</USWDSBannerContent>
</a>
</USWDSBanner>
<div className='position-absolute top-0 right-0 margin-right-3 height-full display-flex'>
<button
className='usa-button usa-button--unstyled'
type='button'
aria-label='Close Banner'
onClick={onClose}
>
<Icon.Close />
</button>
</div>
<USWDSBannerContent id={contentId} isOpen={isOpen}>
<div className='grid-row grid-gap-lg'>
<GuidanceBlock content={leftContent} className='tablet:grid-col-6' />
<GuidanceBlock content={rightContent} className='tablet:grid-col-6' />
</div>
)}
</div>
</USWDSBannerContent>
</USWDSBanner>
);
}
13 changes: 10 additions & 3 deletions app/scripts/components/common/layout-root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import { useDeepCompareEffect } from 'use-deep-compare';
import styled from 'styled-components';
import { Outlet } from 'react-router';
import { reveal } from '@devseed-ui/animation';
import { getBannerFromVedaConfig, getCookieConsentFromVedaConfig } from 'veda';
import {
getBannerFromVedaConfig,
getCookieConsentFromVedaConfig,
getSiteAlertFromVedaConfig
} from 'veda';
import MetaTags from '../meta-tags';
import PageFooter from '../page-footer';
const Banner = React.lazy(() => import('../banner'));
const SiteAlert = React.lazy(() => import('../site-alert'));
const CookieConsent = React.lazy(() => import('../cookie-consent'));

import { LayoutRootContext } from './context';
Expand Down Expand Up @@ -50,6 +55,7 @@ const PageBody = styled.div`
function LayoutRoot(props: { children?: ReactNode }) {
const cookieConsentContent = getCookieConsentFromVedaConfig();
const bannerContent = getBannerFromVedaConfig();
const siteAlertContent = getSiteAlertFromVedaConfig();
const { children } = props;
const [displayCookieConsentForm, setDisplayCookieConsentForm] =
useState<boolean>(true);
Expand All @@ -74,8 +80,9 @@ function LayoutRoot(props: { children?: ReactNode }) {
description={description || appDescription}
thumbnail={thumbnail}
/>
{bannerContent && (
<Banner appTitle={bannerContent.title} {...bannerContent} />
{bannerContent && <Banner {...bannerContent} />}
{siteAlertContent && (
<SiteAlert appTitle={siteAlertContent.title} {...siteAlertContent} />
)}
<NavWrapper
mainNavItems={mainNavItems}
Expand Down
81 changes: 81 additions & 0 deletions app/scripts/components/common/site-alert/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useState } from 'react';
import { Icon } from '@trussworks/react-uswds';
import { decode } from 'he';
import { USWDSSiteAlert } from '$components/common/uswds/site-alert';

const ALERT_KEY = 'dismissedSiteAlertUrl';

function hasExpired(expiryDatetime?: Date): boolean {
if (!expiryDatetime) return false;
const expiryDate = new Date(expiryDatetime);
const currentDate = new Date();
return !!(currentDate > expiryDate);
}

enum SiteAlertType {
info = 'info',
emergency = 'emergency'
}

const infoTypeFlag = SiteAlertType.info;

interface SiteAlertProps {
appTitle: string;
expires?: Date;
content: string;
type?: SiteAlertType;
heading?: string;
showIcon?: boolean;
slim?: boolean;
className?: string;
}

export default function SiteAlert({
appTitle,
expires,
content,
type = infoTypeFlag,
heading,
showIcon = true,
slim = false,
className = ''
}: SiteAlertProps) {
const showAlert = localStorage.getItem(ALERT_KEY) !== content;
const [isOpen, setIsOpen] = useState(showAlert && !hasExpired(expires));

function onClose() {
localStorage.setItem(ALERT_KEY, content);
setIsOpen(false);
}

return (
<div>
{isOpen && (
<div className='position-relative'>
<USWDSSiteAlert
aria-label={`${appTitle} site alert`}
variant={type}
heading={heading}
showIcon={showIcon}
slim={slim}
className={`${className} ${
type !== infoTypeFlag ? 'bg-secondary-lighter' : ''
}`}
>
<div dangerouslySetInnerHTML={{ __html: decode(content) }} />
</USWDSSiteAlert>
<div className='position-absolute top-0 right-0 margin-right-3 height-full display-flex'>
<button
className='usa-button usa-button--unstyled'
type='button'
aria-label={`Close ${appTitle} site alert`}
onClick={onClose}
>
<Icon.Close />
</button>
</div>
</div>
)}
</div>
);
}
10 changes: 0 additions & 10 deletions app/scripts/components/common/uswds/banner.tsx

This file was deleted.

42 changes: 42 additions & 0 deletions app/scripts/components/common/uswds/banner/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import {
Banner,
BannerContent,
BannerButton,
BannerFlag,
BannerHeader,
BannerIcon,
BannerGuidance, MediaBlockBody
} from '@trussworks/react-uswds';

export function USWDSBanner(props) {
return <Banner {...props} />;
}

export function USWDSBannerContent(props) {
return <BannerContent {...props} />;
}

export function USWDSBannerButton(props) {
return <BannerButton {...props} />;
}

export function USWDSBannerFlag(props) {
return <BannerFlag {...props} />;
}

export function USWDSBannerHeader(props) {
return <BannerHeader {...props} />;
}

export function USWDSBannerIcon(props) {
return <BannerIcon {...props} />;
}

export function USWDSBannerGuidance(props) {
return <BannerGuidance {...props} />;
}

export function USWDSMediaBlockBody(props) {
return <MediaBlockBody {...props} />;
}
6 changes: 6 additions & 0 deletions app/scripts/components/common/uswds/site-alert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import { SiteAlert } from '@trussworks/react-uswds';

export function USWDSSiteAlert(props) {
return <SiteAlert {...props} />;
}
Loading
Loading