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

fix: fix cookie banner color (#239) #251

Merged
merged 1 commit into from
Nov 4, 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import { BaseComponentProps } from "../../../../../../../../theme/common/types";
import {
ImageSrc,
StaticImage,
} from "../../../../../../../common/StaticImage/staticImage";
import { ANCHOR_TARGET } from "../../../../../../../Links/common/entities";
import { BaseComponentProps } from "../../../../../../../types";
import { StyledLink } from "./logo.styles";

export interface LogoProps extends BaseComponentProps {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Links/components/Link/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
import NLink from "next/link";
import React, { ReactNode } from "react";
import { isValidUrl } from "../../../../common/utils";
import { BaseComponentProps } from "../../../../theme/common/types";
import { CopyToClipboard } from "../../../common/CopyToClipboard/copyToClipboard";
import { TypographyProps } from "../../../common/Typography/common/entities";
import { BaseComponentProps } from "../../../types";
import { ANCHOR_TARGET, REL_ATTRIBUTE, Url } from "../../common/entities";
import {
isClientSideNavigation,
Expand Down
29 changes: 29 additions & 0 deletions src/components/common/Alert/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { AlertProps } from "@mui/material";
import { COLOR, SEVERITY, VARIANT } from "../../../styles/common/mui/alert";

export const ALERT_PROPS: Record<string, Partial<AlertProps>> = {
FILLED_INK: {
color: COLOR.INK,
variant: VARIANT.FILLED,
},
FILLED_PRIMARY: {
color: COLOR.PRIMARY,
variant: VARIANT.FILLED,
},
FILLED_SMOKE: {
color: COLOR.SMOKE,
variant: VARIANT.FILLED,
},
STANDARD_ERROR: {
severity: SEVERITY.ERROR,
},
STANDARD_INFO: {
severity: SEVERITY.INFO,
},
STANDARD_SUCCESS: {
severity: SEVERITY.SUCCESS,
},
STANDARD_WARNING: {
severity: SEVERITY.WARNING,
},
};
5 changes: 5 additions & 0 deletions src/components/common/Alert/hooks/useTransition/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface UseTransition {
isIn: boolean;
onEnter: () => void;
onExit: () => void;
}
25 changes: 25 additions & 0 deletions src/components/common/Alert/hooks/useTransition/useTransition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useCallback, useState } from "react";
import { UseTransition } from "./types";

/**
* Hook to manage transition state for alert component wrapped in a transition component.
* @param initialState - Initial state of the transition.
* @returns transition state and handlers.
*/
export const useTransition = (initialState?: boolean): UseTransition => {
const [isIn, setIsIn] = useState<boolean>(initialState || false);

const onEnter = useCallback((): void => {
setIsIn(true);
}, []);

const onExit = useCallback((): void => {
setIsIn(false);
}, []);

return {
isIn,
onEnter,
onExit,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AlertProps } from "@mui/material";
import { COLOR, VARIANT } from "../../../../../styles/common/mui/alert";

export const ALERT_PROPS: Partial<AlertProps> = {
color: COLOR.INK,
elevation: 2,
icon: false,
variant: VARIANT.FILLED,
};
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import styled from "@emotion/styled";
import { Alert } from "@mui/material";
import { mediaTabletUp } from "../../../../../styles/common/mixins/breakpoints";
import { white } from "../../../../../styles/common/mixins/colors";
import { textBody400 } from "../../../../../styles/common/mixins/fonts";
import { shadows02 } from "../../../../../styles/common/mixins/shadows";
import { Banner } from "../../banner";

export const CookieBanner = styled(Banner)`
export const StyledAlert = styled(Alert)`
bottom: 0;
box-shadow: ${shadows02};
color: ${white};
flex-direction: column;
gap: 16px;
left: 0;
margin: 8px;
padding: 16px;
position: fixed;
width: calc(100vw - 16px);
z-index: 1100; // Above support fab, below support form.

.MuiAlert-message {
${textBody400};

.MuiLink-root {
color: inherit;
}
}

.MuiAlert-action {
Expand Down
100 changes: 36 additions & 64 deletions src/components/common/Banner/components/CookieBanner/cookieBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ButtonProps, AlertProps as MAlertProps } from "@mui/material";
import React, { forwardRef, Fragment, ReactNode } from "react";
import { AlertProps, Button, Fade } from "@mui/material";
import React, { Fragment, ReactNode, useEffect } from "react";
import { FLAG } from "../../../../../hooks/useFeatureFlag/common/entities";
import { setLocalStorage } from "../../../../../hooks/useLocalStorage/common/utils";
import { useLocalStorage } from "../../../../../hooks/useLocalStorage/useLocalStorage";
import { BaseComponentProps } from "../../../../../theme/common/types";
import { ButtonPrimary } from "../../../Button/components/ButtonPrimary/buttonPrimary";
import { DismissibleBanner } from "../DismissibleBanner/dismissibleBanner";
import { CookieBanner as Banner } from "./cookieBanner.styles";
import { BaseComponentProps } from "../../../../types";
import { useTransition } from "../../../Alert/hooks/useTransition/useTransition";
import { ALERT_PROPS } from "./constants";
import { StyledAlert } from "./cookieBanner.styles";

export interface CookieBannerProps extends BaseComponentProps, MAlertProps {
export interface CookieBannerProps
extends Omit<AlertProps, "children">,
BaseComponentProps {
localStorageKey: string;
message?: ReactNode;
secondaryAction?: ReactNode;
Expand All @@ -20,66 +22,36 @@ export const CookieBanner = ({
message,
secondaryAction,
}: CookieBannerProps): JSX.Element => {
const { isIn, onEnter, onExit } = useTransition();
const localStorage = useLocalStorage(localStorageKey);
const isCookieAccepted = localStorage === FLAG.TRUE;

// Callback fired when the banner requests to be closed.
const onDismiss = (): void => {
setLocalStorage(localStorageKey, FLAG.TRUE);
};
useEffect(() => {
if (localStorage === null) onEnter();
}, [localStorage, onEnter]);

return (
<DismissibleBanner
Alert={Alert}
className={className}
onDismiss={onDismiss}
open={!isCookieAccepted}
slots={{
closeButton: (props) => renderCloseButton(props, secondaryAction),
}}
>
{message}
</DismissibleBanner>
<Fade in={isIn} unmountOnExit>
<StyledAlert
{...ALERT_PROPS}
action={
<Fragment>
<Button
color="primary"
onClick={(): void => {
setLocalStorage(localStorageKey, FLAG.TRUE);
onExit();
}}
variant="contained"
>
Ok, Got It
</Button>
{secondaryAction}
</Fragment>
}
className={className}
>
{message}
</StyledAlert>
</Fade>
);
};

/**
* Return the cookie banner alert.
* @param props - Alert props e.g. "onClick" to close banner.
* @returns alert element.
*/
const Alert = forwardRef<HTMLDivElement, MAlertProps>(function Alert(
{ ...props },
ref
): JSX.Element {
return (
<Banner
color="ink"
elevation={2}
icon={false}
ref={ref}
variant="filled"
{...props}
>
{props.children}
</Banner>
);
});

/**
* Returns the close action component(s).
* @param buttonProps - Button props e.g. "onClick" to close banner.
* @param secondaryAction - Secondary action component.
* @returns close button element(s).
*/
function renderCloseButton(
buttonProps: ButtonProps,
secondaryAction?: ReactNode
): JSX.Element {
return (
<Fragment>
<ButtonPrimary onClick={buttonProps.onClick}>Ok, Got It</ButtonPrimary>
{secondaryAction}
</Fragment>
);
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/common/Breadcrumbs/breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ChevronRightRoundedIcon from "@mui/icons-material/ChevronRightRounded";
import { Link, Breadcrumbs as MBreadcrumbs, Typography } from "@mui/material";
import NLink from "next/link";
import React, { ReactNode } from "react";
import { BaseComponentProps } from "../../../theme/common/types";
import { BaseComponentProps } from "../../types";

export interface Breadcrumb {
path: string;
Expand Down
3 changes: 3 additions & 0 deletions src/components/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface BaseComponentProps {
className?: string;
}
4 changes: 2 additions & 2 deletions src/hooks/useLocalStorage/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { getLocalStorage } from "./common/utils";
* @param key - Local storage key.
* @returns local storage value.
*/
export function useLocalStorage(key: string): string | null {
const [value, setValue] = useState<string | null>(null);
export function useLocalStorage(key: string): string | null | undefined {
const [value, setValue] = useState<string | null>();

useEffect(() => {
setValue(getLocalStorage(key));
Expand Down
24 changes: 24 additions & 0 deletions src/styles/common/mui/alert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AlertProps } from "@mui/material";

export const COLOR: Record<string, AlertProps["color"]> = {
ERROR: "error",
INFO: "info",
INK: "ink",
PRIMARY: "primary",
SMOKE: "smoke",
SUCCESS: "success",
WARNING: "warning",
};

export const SEVERITY: Record<string, AlertProps["severity"]> = {
ERROR: "error",
INFO: "info",
SUCCESS: "success",
WARNING: "warning",
};

export const VARIANT: Record<string, AlertProps["variant"]> = {
FILLED: "filled",
OUTLINED: "outlined",
STANDARD: "standard",
};
8 changes: 8 additions & 0 deletions src/styles/common/mui/icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IconProps } from "@mui/material";

export const FONT_SIZE: Record<string, IconProps["fontSize"]> = {
INHERIT: "inherit",
LARGE: "large",
MEDIUM: "medium",
SMALL: "small",
};
Loading