Skip to content

Commit

Permalink
Refactor admonition component (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianstasik authored Sep 18, 2023
1 parent f7f9c51 commit fb69400
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/components/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Button({ children, variant, ...rest }: ButtonProps) {
className={clsx(
"border font-semibold h-12 px-6 flex items-center hover:no-underline transition-colors",
variant === "primary" &&
"bg-yellow-500 text-gray-900 hover:bg-yellow-600 border-yellow-500 hover:border-yellow-600-hover hover:text-gray-900",
"bg-brand-500 text-gray-900 hover:bg-brand-600 border-brand-500 hover:border-brand-600-hover hover:text-gray-900",
variant === "secondary" &&
"border-gray-200 dark:border-gray-800 text-gray-900 dark:text-gray-50 bg-transparent hover:border-gray-900 dark:hover:border-gray-50 hover:text-gray-900 dark:hover:text-gray-50 aria-selected:border-gray-900 dark:aria-selected:border-gray-50"
)}
Expand Down
34 changes: 17 additions & 17 deletions src/components/HowToSupport/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export default function HowToSupport() {
xmlns="http://www.w3.org/2000/svg"
aria-hidden
>
<g className="fill-yellow-600">
<g className="fill-brand-600">
<path d="M36 8.152H14.667c-2.206 0-4 1.795-4 4v23.334a4.672 4.672 0 0 0 4.666 4.666H36c.737 0 1.333-.597 1.333-1.333V9.485c0-.736-.596-1.333-1.333-1.333ZM17.333 14.82h13.334v9.333H17.333V14.82Zm17.334 22.666H15.333c-1.102 0-2-.897-2-2 0-1.102.898-2 2-2h19.334v4Z" />
<path d="M35.334 8.153h2v31.995h-2z" />
</g>
<path
className="fill-yellow-300"
className="fill-brand-300"
d="M15.038 14.069h18.064v10.389H15.038z"
/>
</svg>
Expand All @@ -67,26 +67,26 @@ export default function HowToSupport() {
>
<g stroke-miterlimit="10" stroke-width="3.2">
<path
className="stroke-yellow-300"
className="stroke-brand-300"
d="m16 24.258 7.04-8.448a3.201 3.201 0 0 1 2.46-1.152H40"
/>
<path
className="stroke-yellow-600"
className="stroke-brand-600"
d="m16 24.658 7.04 8.448a3.201 3.201 0 0 0 2.46 1.152H40"
/>
<path
className="stroke-yellow-300"
className="stroke-brand-300"
stroke-linecap="round"
d="M8 24.258h8l2.5-3"
/>
<path
className="stroke-yellow-300"
className="stroke-brand-300"
stroke-linecap="round"
stroke-linejoin="round"
d="m33.6 8.258 6.4 6.4-6.4 6.4"
/>
<path
className="stroke-yellow-600"
className="stroke-brand-600"
stroke-linecap="round"
stroke-linejoin="round"
d="m33.6 27.459 6.4 6.4-6.4 6.4"
Expand All @@ -108,11 +108,11 @@ export default function HowToSupport() {
aria-hidden
>
<path
className="fill-yellow-600"
className="fill-brand-600"
d="M27.845 28.087c-.04-1.77-.161-3.66-.483-5.47-.16-.764-.241-1.408-.241-2.091v-9.292c0-.402-.161-.764-.443-1.086a1.515 1.515 0 0 0-1.207-.443c-.804.08-1.407.765-1.407 1.65v7.32a.705.705 0 0 1-.684.684.705.705 0 0 1-.684-.684V8.5c0-.765-.523-1.448-1.287-1.569a1.5 1.5 0 0 0-1.77 1.488v8.769a.678.678 0 0 1-.684.684.705.705 0 0 1-.683-.684V6.85c0-.765-.523-1.448-1.288-1.57a1.5 1.5 0 0 0-1.77 1.49v9.934a.678.678 0 0 1-.683.684.705.705 0 0 1-.684-.684V8.5c0-.765-.523-1.448-1.287-1.569a1.5 1.5 0 0 0-1.77 1.488V19.48s.282-.04.563-.04l5.109.12c1.488.04 2.855 1.126 2.976 2.615.08.884-.241 1.73-.885 2.333-.442.442-1.005.724-1.609.764l-.603.08.442.443c1.69 1.73 2.092 3.982 1.971 5.39a.663.663 0 0 1-.684.603h-.12c-.362-.04-.644-.402-.604-.764 0-.161.322-3.862-3.459-5.632l-.402-.2-.241 1.568a.627.627 0 0 1-.765.563.627.627 0 0 1-.563-.764l.362-2.293a.67.67 0 0 1 .724-.563l3.58.161c.443 0 .805-.16 1.086-.442.322-.322.483-.765.443-1.247-.08-.765-.765-1.328-1.529-1.328l-5.189-.12a2.814 2.814 0 0 0-2.775 2.413l-.563 4.344c-.241 1.81.322 3.66 1.528 5.068l3.982 4.545c.161.202.282.443.282.724v3.218c0 .684.563 1.247 1.247 1.247h7.522c.683 0 1.247-.563 1.247-1.247v-2.896c-.071-1.576 2.716-10.056 3.982-10.056Z"
/>
<path
className="fill-yellow-300"
className="fill-brand-300"
d="M37.582 28.309c-1.983-1.37-4.72-.897-6.42.802l-.991.992-1.133-1.133c-2.172-2.172-5.901-1.936-7.695.802-1.37 1.983-.897 4.72.802 6.42l7.081 7.081c.52.52 1.37.52 1.889 0l7.27-7.27c2.124-2.171 1.888-5.9-.803-7.694Z"
/>
</svg>
Expand All @@ -130,11 +130,11 @@ export default function HowToSupport() {
aria-hidden
>
<path
className="fill-yellow-600"
className="fill-brand-600"
d="M26.667 10.82a13.28 13.28 0 0 0-13.14 11.067A8 8 0 0 0 16 37.487h10.667a13.333 13.333 0 0 0 0-26.667Z"
/>
<path
className="fill-yellow-300"
className="fill-brand-300"
d="M28 44.485h-8v-12h-5.333L24 23.152l9.333 9.333H28v12Z"
/>
</svg>
Expand All @@ -152,15 +152,15 @@ export default function HowToSupport() {
aria-hidden
>
<path
className="fill-yellow-600"
className="fill-brand-600"
d="M33.786 25.04c1.08-.899 2.67-2.41 2.67-2.41l2.057-2a1.11 1.11 0 0 0-.614-1.891l-9.27-1.355-4.149-8.401a1.108 1.108 0 0 0-1.99 0l-4.146 8.4-9.272 1.348a1.107 1.107 0 0 0-.615 1.892l6.71 6.533-1.579 9.242a1.111 1.111 0 0 0 1.6 1.166c7.437-3.91 12.426-7.383 18.598-12.524Z"
/>
<path
className="fill-white"
d="m28.437 26.529 4.186-.22-.805 5.896-5.675 4.568-1.691-5.434 3.985-4.81Z"
/>
<path
className="fill-yellow-300"
className="fill-brand-300"
d="M30.849 24.258a8 8 0 0 0-8 8 8 8 0 0 0 8 8 8 8 0 0 0 8-8 8 8 0 0 0-8-8Zm5.333 8.667a.667.667 0 0 1-.667.667h-3.333v3.333a.667.667 0 0 1-.667.667h-1.333a.667.667 0 0 1-.667-.667v-3.333h-3.333a.667.667 0 0 1-.667-.667v-1.333c0-.368.299-.667.667-.667h3.333v-3.333c0-.368.299-.667.667-.667h1.333c.368 0 .667.299.667.667v3.333h3.333c.368 0 .667.299.667.667v1.333Z"
/>
</svg>
Expand All @@ -180,19 +180,19 @@ export default function HowToSupport() {
aria-hidden
>
<path
className="fill-yellow-600"
className="fill-brand-600"
d="M19.733 8.32a3.2 3.2 0 1 0 0 6.4h3.2v-3.2a3.2 3.2 0 0 0-3.2-3.2ZM19.733 16.855H11.2a3.2 3.2 0 1 0 0 6.4h8.533a3.2 3.2 0 0 0 0-6.4Z"
/>
<path
className="fill-yellow-300"
className="fill-brand-300"
d="M39.997 20.054a3.2 3.2 0 1 0-6.4 0v3.2h3.2a3.2 3.2 0 0 0 3.2-3.2ZM31.467 20.053V11.52a3.2 3.2 0 0 0-6.4 0v8.533a3.2 3.2 0 0 0 6.4 0Z"
/>
<path
className="fill-yellow-600"
className="fill-brand-600"
d="M28.267 40.319a3.2 3.2 0 1 0 0-6.4h-3.2v3.2a3.2 3.2 0 0 0 3.2 3.2ZM28.267 31.787H36.8a3.2 3.2 0 0 0 0-6.4h-8.533a3.2 3.2 0 1 0 0 6.4Z"
/>
<path
className="fill-yellow-300"
className="fill-brand-300"
d="M8 28.587a3.2 3.2 0 1 0 6.4 0v-3.2h-3.2a3.2 3.2 0 0 0-3.2 3.2ZM16.533 28.587v8.533a3.2 3.2 0 0 0 6.4 0v-8.533a3.2 3.2 0 0 0-6.4 0Z"
/>
</svg>
Expand Down
2 changes: 1 addition & 1 deletion src/components/LatestNews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function LatestNews({ recentPosts }) {
<time
dateTime={date}
itemProp="datePublished"
className="text-yellow-700 dark:text-yellow-500 font-bold uppercase"
className="text-brand-700 dark:text-brand-500 font-bold uppercase"
>
{formattedDate}
</time>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Supporters/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function SupporterType({ type, withSeparator, count }: SupporterTypeProps) {
</span>
{count && (
<sup
className="ml-1 mt-2 text-yellow-700 dark:text-yellow-600 text-base font-bold"
className="ml-1 mt-2 text-brand-700 dark:text-brand-600 text-base font-bold"
aria-hidden
>
{count}
Expand Down
4 changes: 2 additions & 2 deletions src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ html[data-theme="light"] {
}

html[data-theme="dark"] {
--ifm-color-primary: theme("colors.yellow.500");
--ifm-color-primary: theme("colors.brand.500");
--ifm-background-color: theme("colors.blue.950");
--ifm-navbar-search-input-background-color: theme("colors.gray.800");
--search-local-hit-background: theme("colors.blue.900");
Expand All @@ -26,7 +26,7 @@ html[data-theme="dark"] {
}

html[data-theme="light"] {
--ifm-color-primary: theme("colors.yellow.700");
--ifm-color-primary: theme("colors.brand.700");
--ifm-background-color: theme("colors.gray.50");
--ifm-navbar-search-input-background-color: theme("colors.gray.150");
--ifm-navbar-search-input-icon: url('data:image/svg+xml;utf8,<svg fill="%238590A2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" height="16px" width="16px"><path d="M6.02945,10.20327a4.17382,4.17382,0,1,1,4.17382-4.17382A4.15609,4.15609,0,0,1,6.02945,10.20327Zm9.69195,4.2199L10.8989,9.59979A5.88021,5.88021,0,0,0,12.058,6.02856,6.00467,6.00467,0,1,0,9.59979,10.8989l4.82338,4.82338a.89729.89729,0,0,0,1.29912,0,.89749.89749,0,0,0-.00087-1.29909Z" /></svg>');
Expand Down
4 changes: 2 additions & 2 deletions src/icons/dots.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/icons/expand.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/icons/humidity.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/icons/layers.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/icons/scale.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/pages/supporters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function SupportersPage() {
>
{type}
<sup
className="text-yellow-700 dark:text-yellow-600 font-bold text-base ml-2 mt-2"
className="text-brand-700 dark:text-brand-600 font-bold text-base ml-2 mt-2"
aria-hidden
>
{supporters.length}
Expand Down
137 changes: 137 additions & 0 deletions src/theme/Admonition/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import React, { type ReactNode } from "react";
import clsx from "clsx";
import type { Props } from "@theme/Admonition";

function NoteIcon() {
return (
<svg viewBox="0 0 14 16">
<path
fillRule="evenodd"
d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"
/>
</svg>
);
}

function DangerIcon() {
return (
<svg viewBox="0 0 12 16">
<path
fillRule="evenodd"
d="M5.05.31c.81 2.17.41 3.38-.52 4.31C3.55 5.67 1.98 6.45.9 7.98c-1.45 2.05-1.7 6.53 3.53 7.7-2.2-1.16-2.67-4.52-.3-6.61-.61 2.03.53 3.33 1.94 2.86 1.39-.47 2.3.53 2.27 1.67-.02.78-.31 1.44-1.13 1.81 3.42-.59 4.78-3.42 4.78-5.56 0-2.84-2.53-3.22-1.25-5.61-1.52.13-2.03 1.13-1.89 2.75.09 1.08-1.02 1.8-1.86 1.33-.67-.41-.66-1.19-.06-1.78C8.18 5.31 8.68 2.45 5.05.32L5.03.3l.02.01z"
/>
</svg>
);
}

function CautionIcon() {
return (
<svg viewBox="0 0 16 16">
<path
fillRule="evenodd"
d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"
/>
</svg>
);
}

type AdmonitionConfig = {
iconComponent: React.ComponentType;
className: string;
label: ReactNode;
};

const AdmonitionConfigs: Record<string, AdmonitionConfig> = {
note: {
className:
"bg-sky-100 border-sky-300 text-sky-700 dark:bg-sky-950 dark:border-sky-700 dark:text-sky-200",
iconComponent: NoteIcon,
label: "Note",
},
danger: {
className:
"bg-red-100 border-red-300 text-red-700 dark:bg-red-950 dark:border-red-700 dark:text-red-200",
iconComponent: DangerIcon,
label: "Danger",
},
warning: {
className:
"bg-yellow-100 border-yellow-500 text-yellow-700 dark:bg-yellow-950 dark:border-yellow-700 dark:text-yellow-200",
iconComponent: CautionIcon,
label: "Warning",
},
};

function getAdmonitionConfig(type: string) {
const config = (AdmonitionConfigs as { [key: string]: AdmonitionConfig })[
type
];

if (config) {
return config;
}

return AdmonitionConfigs.note;
}

// Workaround because it's difficult in MDX v1 to provide a MDX title as props
// See https://github.com/facebook/docusaurus/pull/7152#issuecomment-1145779682
function extractMDXAdmonitionTitle(children: ReactNode): {
mdxAdmonitionTitle: ReactNode | undefined;
rest: ReactNode;
} {
const items = React.Children.toArray(children);
const mdxAdmonitionTitle = items.find(
(item) =>
React.isValidElement(item) &&
(item.props as { mdxType: string } | null)?.mdxType ===
"mdxAdmonitionTitle"
);
const rest = <>{items.filter((item) => item !== mdxAdmonitionTitle)}</>;
return {
mdxAdmonitionTitle,
rest,
};
}

function processAdmonitionProps(props: Props): Props {
const { mdxAdmonitionTitle, rest } = extractMDXAdmonitionTitle(
props.children
);
return {
...props,
title: props.title ?? mdxAdmonitionTitle,
children: rest,
};
}

export default function Admonition(props: Props) {
const {
children,
type,
title,
icon: iconProp,
} = processAdmonitionProps(props);

const typeConfig = getAdmonitionConfig(type);
const titleLabel = title ?? typeConfig.label;
const { iconComponent: IconComponent } = typeConfig;
const icon = iconProp ?? <IconComponent />;
return (
<div
className={clsx(
"flex flex-col py-2 px-3 not-prose border [&+&]:mt-3",
typeConfig.className
)}
role="alert"
>
<div className="flex gap-3 font-bold">
<span className="flex w-4 fill-current" aria-hidden>
{icon}
</span>
{titleLabel}
</div>
<div>{children}</div>
</div>
);
}
2 changes: 1 addition & 1 deletion src/theme/BlogLastPost/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function BlogLastPost({ item }) {
<time
dateTime={date}
itemProp="datePublished"
className="text-yellow-700 dark:text-yellow-500 uppercase font-bold"
className="text-brand-700 dark:text-brand-500 uppercase font-bold"
>
{formattedDate}
</time>
Expand Down
2 changes: 1 addition & 1 deletion src/theme/BlogListItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function BlogListItem({ item }) {
<time
dateTime={date}
itemProp="datePublished"
className="text-yellow-700 dark:text-yellow-500 uppercase font-bold"
className="text-brand-700 dark:text-brand-500 uppercase font-bold"
>
{formattedDate}
</time>
Expand Down
Loading

0 comments on commit fb69400

Please sign in to comment.