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(SLB-438): prestyling on messages #290

Merged
merged 3 commits into from
Jun 28, 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
7 changes: 6 additions & 1 deletion packages/ui/src/components/Atoms/List.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ul li {
@apply !text-base !font-medium !text-gray-900;
@apply text-base !font-medium !text-gray-900;
}

ul {
Expand All @@ -20,6 +20,11 @@ ul li::marker {
@apply !text-gray-500;
}

/* Remove hardcoded colors on lists within messages so they can be set to their message type inherited color scheme */
ul li.messages::marker {
@apply !text-inherit;
}

.list-style--arrows li::before {
position: absolute;
left: -1.25rem;
Expand Down
38 changes: 38 additions & 0 deletions packages/ui/src/components/Molecules/Messages.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Meta, StoryObj } from '@storybook/react';
import React from 'react';

import { Messages as Component } from './Messages';

export default {
parameters: {
layout: 'fullscreen',
},
component: Component,
} satisfies Meta<typeof Component>;

export const Info: StoryObj<typeof Component> = {
parameters: {
location: new URL('local:/gatsby-turbo'),
},
args: {
messages: [
'<p>This is an Info message, <a href=#>linked item</a></p><ul><li>Fribourgeoise</li><li>Moitié-Moitié</li></ul>',
'<p>This is a Warning message, <a href=#>linked item</a></p>',
'<p>This is a Danger message, <a href=#>linked item</a></p>',
'<p>This is a Success message, <a href=#>linked item</a></p>',
],
messageComponents: [
<div key={'test'}>
{'This page is not available in the requested language.'}{' '}
<a
href="#"
onClick={() => {
window.history.back();
}}
>
{'Go back'}
</a>
</div>,
],
},
};
129 changes: 117 additions & 12 deletions packages/ui/src/components/Molecules/Messages.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,132 @@
import { Html, Markup } from '@custom/schema';
import React, { ReactNode } from 'react';
import clsx from 'clsx';
import React, {
PropsWithChildren,
ReactNode,
useEffect,
useState,
} from 'react';

// TODO: Style, add stories.
import { unorderedItems } from '../Organisms/PageContent/BlockMarkup';

export function Messages(props: {
messages: Array<string>;
messageComponents?: Array<ReactNode>;
}) {
const [displayMessages, setDisplayMessages] = useState<string[]>([]);
const [messageComponents, setMessageComponents] = React.useState<
Array<ReactNode>
>([]);

useEffect(() => {
setDisplayMessages(props.messages);
props.messageComponents && setMessageComponents(props.messageComponents);
}, [props.messages]);

const handleRemoveMessage = (index: number) => {
const newMessages = displayMessages.filter((_, i) => i !== index);
setDisplayMessages(newMessages);
storeMessages(newMessages);
};

return (
<div>
{buildMessages(props.messages)}
{props.messageComponents}
<div className="container-page">
<div className="container-content">
{buildMessages(displayMessages, handleRemoveMessage)}
{buildMessages(messageComponents)}
</div>
</div>
);
}

export const buildMessages = (messages: Array<string>) => (
<>
{messages.map((message, index) => (
<Html key={index} markup={message as Markup} />
))}
</>
);
export const buildMessages = (
messages: Array<string> | Array<ReactNode>,
handleRemoveMessage?: (index: number) => void,
) => {
return (
<>
{messages.map((message, index) => (
<div
key={index}
className="flex items-center p-4 my-4 text-blue-800 border-t-4 border-blue-300 bg-blue-50"
role="alert"
aria-live="polite"
>
<svg
className="mr-3 shrink-0 w-4 h-4"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 20 20"
>
<path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM9.5 4a1.5 1.5 0 1 1 0 3 1.5 1.5 0 0 1 0-3ZM12 15H8a1 1 0 0 1 0-2h1v-3H8a1 1 0 0 1 0-2h2a1 1 0 0 1 1 1v4h1a1 1 0 0 1 0 2Z" />
</svg>
<span className="sr-only">Info</span>
<div className="text-sm font-medium prose-a:font-semibold prose-a:underline">
{typeof message === 'string' ? (
<Html
key={index}
markup={message as Markup}
plugins={[unorderedItems]}
components={{
li: ({
unordered,
children,
className,
...props
}: PropsWithChildren<{
unordered?: boolean;
className?: string;
}>) => {
return (
<li
{...props}
className={clsx(className, {
'!text-blue-800 ml-5 mt-1 mb-1 list-disc messages text-sm font-medium':
unordered,
})}
>
{children}
</li>
);
},
}}
/>
) : (
message
)}
</div>
{handleRemoveMessage && (
<button
type="button"
className="ms-auto -m-1.5 bg-blue-50 text-blue-500 rounded-lg focus:ring-2 focus:ring-blue-400 p-1.5 hover:bg-blue-200 inline-flex items-center justify-center h-8 w-8"
data-dismiss-target={`#alert-${index + 1}`}
onClick={() => handleRemoveMessage(index)}
aria-label={`Close message ${index + 1}`}
>
<span className="sr-only">Close</span>
<svg
className="w-3 h-3"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 14 14"
>
<path
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"
/>
</svg>
</button>
)}
</div>
))}
</>
);
};

export const storeMessages = (messages: Array<string>): void => {
localStorage.setItem('messages', JSON.stringify(messages));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Plugin } from 'unified';

import { FadeUp } from '../../Molecules/FadeUp';

const unorderedItems: Plugin<[], Element> = () => (tree) => {
export const unorderedItems: Plugin<[], Element> = () => (tree) => {
selectAll('ul > li', tree).forEach((node) => {
node.properties!.unordered = true;
});
Expand Down
Loading