0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Implemented very simple version of Breadcrumb component

This commit is contained in:
Sodbileg Gansukh 2024-10-09 16:53:56 +01:00
parent 09b4aceb64
commit 5632f27c60
4 changed files with 81 additions and 14 deletions

View file

@ -0,0 +1,25 @@
import type {Meta, StoryObj} from '@storybook/react';
import React from 'react';
import Breadcrumb, {BreadcrumbItem} from './Breadcrumb';
const meta = {
title: 'Global / Breadcrumb',
component: Breadcrumb,
tags: ['autodocs']
} satisfies Meta<typeof Breadcrumb>;
export default meta;
type Story = StoryObj<typeof Breadcrumb>;
export const Default: Story = {
args: {
children: (
<Breadcrumb>
<BreadcrumbItem>f</BreadcrumbItem>
<BreadcrumbItem>f</BreadcrumbItem>
<BreadcrumbItem>f</BreadcrumbItem>
</Breadcrumb>
)
}
};

View file

@ -0,0 +1,37 @@
import React, {Children, isValidElement, useId} from 'react';
export type BreadcrumbProps = {
children: React.ReactNode
}
export type BreadcrumbItemProps = {
children: React.ReactNode
onClick?: () => void;
}
const Breadcrumb: React.FC<BreadcrumbProps> = ({children}) => {
const id = useId();
const items = Children.toArray(children);
return (
<div>
{items.map((child, index) => {
if (isValidElement(child)) {
return (
<span key={id}>
{child}
{index < items.length - 1 && <span>/</span>}
</span>
);
}
return child;
})}
</div>
);
};
export const BreadcrumbItem: React.FC<BreadcrumbItemProps> = ({children, onClick}) => {
return <span onClick={onClick}>{children}</span>;
};
export default Breadcrumb;

View file

@ -101,6 +101,6 @@ export const BreadcrumbsToolbar: Story = {
args: {
...Default.args,
previewToolbarTabs: undefined,
previewToolbarBreadcrumbs: [{label: 'Previous', onClick: () => {}}, {label: 'Current'}]
previewToolbarBreadcrumbs: [{children: 'Previous', onClick: () => {}}, {children: 'Current'}]
}
};

View file

@ -4,7 +4,8 @@ import React, {useEffect, useState} from 'react';
import useGlobalDirtyState from '../../hooks/useGlobalDirtyState';
import {confirmIfDirty} from '../../utils/modals';
import {ButtonColor, ButtonProps} from '../Button';
import Breadcrumbs, {BreadcrumbItem} from '../Breadcrumbs';
// import Breadcrumbs, {BreadcrumbItem} from '../Breadcrumbs';
import Breadcrumb, {BreadcrumbItem, BreadcrumbItemProps} from '../Breadcrumb';
import ButtonGroup from '../ButtonGroup';
import Heading, {HeadingLevel} from '../Heading';
import Icon from '../Icon';
@ -34,7 +35,7 @@ export interface PreviewModalProps {
deviceSelector?: boolean;
siteLink?: string;
previewToolbarURLs?: SelectOption[];
previewToolbarBreadcrumbs?: BreadcrumbItem[];
previewToolbarBreadcrumbs?: BreadcrumbItemProps[];
previewBgColor?: 'grey' | 'white' | 'greygradient';
selectedURL?: string;
previewToolbarTabs?: Tab[];
@ -91,8 +92,8 @@ export const PreviewModalContent: React.FC<PreviewModalProps> = ({
afterClose,
onSelectURL,
onSelectDesktopView,
onSelectMobileView,
onBreadcrumbsBack
onSelectMobileView
// onBreadcrumbsBack
}) => {
const modal = useModal();
const {setGlobalDirtyState} = useGlobalDirtyState();
@ -153,15 +154,19 @@ export const PreviewModalContent: React.FC<PreviewModalProps> = ({
onTabChange={onSelectURL!}
/>;
} else if (previewToolbarBreadcrumbs) {
toolbarLeft = <Breadcrumbs
activeItemClassName='hidden md:!block md:!visible'
containerClassName='whitespace-nowrap'
itemClassName='hidden md:!block md:!visible'
items={previewToolbarBreadcrumbs}
separatorClassName='hidden md:!block md:!visible'
backIcon
onBack={onBreadcrumbsBack}
/>;
// toolbarLeft = <Breadcrumbs
// activeItemClassName='hidden md:!block md:!visible'
// containerClassName='whitespace-nowrap'
// itemClassName='hidden md:!block md:!visible'
// items={previewToolbarBreadcrumbs}
// separatorClassName='hidden md:!block md:!visible'
// backIcon
// onBack={onBreadcrumbsBack}
// />;
toolbarLeft = <Breadcrumb>
{previewToolbarBreadcrumbs.map(item => <BreadcrumbItem onClick={item.onClick}>{item.children}</BreadcrumbItem>
)}
</Breadcrumb>;
}
const selectedIconColorClass = 'text-black dark:text-green';