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

Announcement component using USWDS #1031

Merged
merged 25 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bfa2b31
Use SiteAlert component from uswds
hanbyul-here Jul 2, 2024
242fa7e
Safelist component selectors
dzole0311 Jul 3, 2024
8986863
Replace importing the whole uswds package with usa-site-alert
dzole0311 Jul 9, 2024
d26df3e
Use SiteAlert component from uswds
hanbyul-here Jul 2, 2024
090ecf7
Safelist component selectors
dzole0311 Jul 3, 2024
a5be0c0
Replace importing the whole uswds package with usa-site-alert
dzole0311 Jul 9, 2024
121c8e5
Merge branch 'main' into 288-announcement
dzole0311 Jul 9, 2024
0eb817b
Merge branch '288-announcement' of https://github.com/NASA-IMPACT/ved…
dzole0311 Jul 9, 2024
8339de5
Update import
dzole0311 Jul 9, 2024
9d5c3b1
Merge branch 'main' into 288-announcement
hanbyul-here Jul 12, 2024
6456ca1
Site Alert
hanbyul-here Jul 12, 2024
223481a
Add announcement banner on top of the website
hanbyul-here Jul 15, 2024
c4f85f0
Make Banner part of configuration
hanbyul-here Jul 19, 2024
fb8bf3f
Move style, rename component to banner
hanbyul-here Jul 19, 2024
1928800
Merge branch 'main' into 288-announcement
hanbyul-here Jul 22, 2024
5cdd840
Remove custom styles for components (replace it with uswds util class),
hanbyul-here Jul 22, 2024
60a57d6
Comment out index css, add purgecss for utilities
hanbyul-here Jul 22, 2024
8253b89
Add types, replace SmartLink to a tag, lint fix
hanbyul-here Jul 22, 2024
266ffbe
Fix Banner config
hanbyul-here Jul 24, 2024
508bd47
Add Banner docs
hanbyul-here Jul 24, 2024
6f61fbc
Rename Banner attributes
hanbyul-here Jul 24, 2024
57ef36e
Remove uswds css
hanbyul-here Jul 24, 2024
35b7e71
Make a separate folder to wrap uswds components
hanbyul-here Jul 26, 2024
30f5475
Merge branch 'main' into 288-announcement
hanbyul-here Jul 26, 2024
737efff
Make purgecss work only on build
hanbyul-here Jul 26, 2024
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
63 changes: 63 additions & 0 deletions app/scripts/components/common/banner/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, {useState} from "react";
import { Banner as USWDSBanner, BannerContent, Icon } from "@trussworks/react-uswds";

const BANNER_KEY = 'dismissedBannerUrl';

function hasExpired(expiryDatetime) {
const expiryDate = new Date(expiryDatetime);
const currentDate = new Date();
return !!(currentDate > expiryDate);
}

enum BannerType {
info = 'info',
warning ='warning'
}

const infoTypeFlag = BannerType.info;
interface BannerProps {
appTitle: string,
expiryDate: Date,
actionUrl: string,
contents: string,
type?: BannerType
}

export default function Banner({appTitle, expiryDate, actionUrl, contents, type = infoTypeFlag }: BannerProps) {

const showBanner = localStorage.getItem(BANNER_KEY) !== actionUrl;
const [isOpen, setIsOpen] = useState(showBanner && !(hasExpired(expiryDate)));

function onClose () {
localStorage.setItem(
BANNER_KEY,
actionUrl
);
setIsOpen(false);
}

return (
<div>
{isOpen &&
(<div className='position-relative'>
hanbyul-here marked this conversation as resolved.
Show resolved Hide resolved
<USWDSBanner aria-label={appTitle} className={type !== infoTypeFlag? 'bg-secondary-lighter': ''}>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Floagging that this component uses uswds default color theme now: https://designsystem.digital.gov/design-tokens/color/theme-tokens/

I don't want to drag this PR with another configuration setup especially considering there hasn't been a request for a different type of banner. (But then it might be better to just get rid of this special styling for now?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Until needed I'm in support of the default theme 👍

<a href={actionUrl} target='_blank' rel='noreferrer'>
<BannerContent className='padding-top-1 padding-bottom-1' isOpen={true}>
<p dangerouslySetInnerHTML={{ __html: contents }} />
</BannerContent>
</a>
</USWDSBanner>
<div className='position-absolute top-0 right-0 margin-right-3 height-full display-flex'>
<button
className='usa-button usa-button--secondary usa-button--unstyled'
type='button'
aria-label='Close Banner'
onClick={onClose}
>
<Icon.Close />
</button>
</div>
</div>)}
</div>
);
}
5 changes: 3 additions & 2 deletions app/scripts/components/common/layout-root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import React, { ReactNode, useContext, useCallback } from 'react';
import { useDeepCompareEffect } from 'use-deep-compare';
import styled from 'styled-components';
import { Outlet } from 'react-router';

import { reveal } from '@devseed-ui/animation';

import MetaTags from '../meta-tags';
import PageFooter from '../page-footer';
import Banner from '../banner';
import { LayoutRootContext } from './context';

import { useGoogleTagManager } from '$utils/use-google-tag-manager';
Expand Down Expand Up @@ -36,7 +36,7 @@ function LayoutRoot(props: { children?: ReactNode }) {

useGoogleTagManager();

const { title, thumbnail, description, hideFooter } =
const { title, thumbnail, description, banner, hideFooter } =
useContext(LayoutRootContext);

const truncatedTitle =
Expand All @@ -50,6 +50,7 @@ function LayoutRoot(props: { children?: ReactNode }) {
description={description || appDescription}
thumbnail={thumbnail}
/>
{banner && <Banner appTitle={title} {...banner} />}
<NavWrapper />
<PageBody id={PAGE_BODY_ID} tabIndex={-1}>
<Outlet />
Expand Down
9 changes: 5 additions & 4 deletions app/scripts/components/common/smart-link.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React from 'react';
import React, { ReactNode } from 'react';
import { Link } from 'react-router-dom';

import { getLinkProps } from '$utils/url';

interface SmartLinkProps {
to: string;
children?: ReactNode;
}

/**
* Switches between a `a` and a `Link` depending on the url.
*/
export default function SmartLink(props: SmartLinkProps) {
const { to, ...rest } = props;
const { to, children, ...rest } = props;
const isExternalLink = /^https?:\/\//.test(to);
const linkProps = getLinkProps(to);
return isExternalLink ? (
<a {...linkProps} {...rest} />
<a {...linkProps} {...rest}> {children} </a>
) : (
<Link {...linkProps} {...rest} />
<Link {...linkProps} {...rest}> {children} </Link>
);
}

Expand Down
10 changes: 8 additions & 2 deletions app/scripts/components/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Button } from '@devseed-ui/button';
import { glsp, listReset, media, themeVal } from '@devseed-ui/theme-provider';
import { Heading } from '@devseed-ui/typography';
import { CollecticonChevronRightSmall } from '@devseed-ui/collecticons';
import { getOverride } from 'veda';
import { getOverride, getBanner } from 'veda';

import rootCoverImage from '../../../graphics/layout/root-welcome--cover.jpg';

Expand Down Expand Up @@ -131,9 +131,15 @@ const getCoverProps = () => {
function RootHome() {
const { show: showFeedbackModal } = useFeedbackModal();

const banner = getBanner();
const renderBanner = !!banner && banner.contents && banner.actionUrl && banner.expiryDate;

return (
<PageMainContent>
<LayoutProps title='Welcome' />
<LayoutProps
title='Welcome'
banner={renderBanner? {...banner}: null}
/>

<ComponentOverride with='homeHero'>
<PageHeroHome
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createRoot } from 'react-dom/client';
import T from 'prop-types';
import { BrowserRouter, Route, Routes, useLocation } from 'react-router-dom';

import '@trussworks/react-uswds/lib/index.css';
// import '@trussworks/react-uswds/lib/index.css';
hanbyul-here marked this conversation as resolved.
Show resolved Hide resolved
import '$styles/styles.scss';

import { userPages } from 'veda';
Expand Down
4 changes: 4 additions & 0 deletions app/scripts/styles/styles.scss
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
@forward 'uswds-theme';

@use "uswds-utilities";
@use "usa-banner";
@use "usa-button";
6 changes: 6 additions & 0 deletions mock/veda.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ module.exports = {
one: 'Story',
other: 'Stories'
}
},
banner: {
text: 'Read the new data insight on using EMIT and AVIRIS-3 for monitoring large methane emission events.',
url: 'stories/emit-and-aviris-3',
expires: '2024-08-03T12:00:00-04:00',
type: 'info'
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@babel/preset-env": "^7.16.0",
"@babel/preset-react": "^7.16.0",
"@babel/preset-typescript": "^7.18.6",
"@fullhuman/postcss-purgecss": "^6.0.0",
"@mdx-js/mdx": "^2.0.0",
"@parcel/packager-raw-url": "2.7.0",
"@parcel/packager-ts": "2.12.0",
Expand Down Expand Up @@ -86,8 +87,8 @@
"jest-css-modules-transform": "^4.3.0",
"lint-staged": "14.0.1",
"parcel": "^2.12.0",
"parcel-resolver-ignore": "^2.1.3",
"parcel-resolver-alias": "link:./parcel-resolver-alias",
"parcel-resolver-ignore": "^2.1.3",
"parcel-resolver-veda": "link:./parcel-resolver-veda",
"parcel-transformer-mdx": "link:./parcel-transformer-mdx",
"parcel-transformer-mdx-frontmatter": "link:./parcel-transformer-mdx-frontmatter",
Expand Down
20 changes: 20 additions & 0 deletions parcel-resolver-veda/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ export interface LayerInfo {
id: string;
name: string;
}
/**
* Not exporting this type
* Since we are moving forward to ditching VEDA faux module
*/

enum BannerType {
info = 'info',
warning ='warning'
}

const infoTypeFlag = BannerType.info;
interface BannerData {
expiryDate: Date,
actionUrl: string,
contents: string,
type?: BannerType
}


/**
* Named exports: datasets.
Expand Down Expand Up @@ -294,6 +312,8 @@ export interface LayerInfo {

export const getBoolean: (variable: string) => boolean;

export const getBanner: () => BannerData | undefined;

/**
* List of custom user defined pages.
*/
Expand Down
6 changes: 5 additions & 1 deletion parcel-resolver-veda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ module.exports = new Resolver({
logger
)},
strings: ${JSON.stringify(withDefaultStrings(result.strings))},
booleans: ${JSON.stringify(withDefaultStrings(result.booleans))}
booleans: ${JSON.stringify(withDefaultStrings(result.booleans))},
banner: ${JSON.stringify(result.banner)}
};

export const theme = ${JSON.stringify(result.theme) || null};
Expand All @@ -211,6 +212,9 @@ module.exports = new Resolver({
export const getString = (variable) => config.strings[variable];
export const getBoolean = (variable) => config.booleans[variable];

export const getConfig = () => config;
export const getBanner = () => config.banner;

export const datasets = ${generateMdxDataObject(datasetsImportData)};
export const stories = ${generateMdxDataObject(storiesImportData)};
`;
Expand Down
10 changes: 10 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@ module.exports = {
plugins: [
require('autoprefixer'),
require('postcss-import'),
require('@fullhuman/postcss-purgecss')({
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The styles doesn't get hot-reloaded unfortunately :[ (I have to restart the dev server if I want to include a new class name etc.)

content: [
'./app/**/*.{js,jsx,ts,tsx}',
'@trussworks/react-uswds/lib/index.css'
],
safelist: {
deep: [/usa-banner$/],
greedy: [/^usa-banner/]
}
})
]
};
Loading
Loading