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

[DST-407]: enhance command menu #3920

Merged
merged 84 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
2f084f5
first state
sarahgm May 22, 2024
b538823
new file
sarahgm May 22, 2024
03fd1a6
add utils file with iterate tokens function
sarahgm May 22, 2024
fe31b63
add color tokens
sarahgm May 22, 2024
37f6411
add copy for tokens
sarahgm May 22, 2024
e3c5c0a
fix searching lowercase
sarahgm May 22, 2024
6622e0b
save
sarahgm May 22, 2024
393b8e9
add commands
sarahgm May 22, 2024
15946bd
save
sarahgm May 22, 2024
97ad1c0
save
sarahgm May 23, 2024
e9f347e
improvement
sarahgm May 23, 2024
52feb3b
....
sarahgm May 23, 2024
36bdbd0
save remove demo
sarahgm May 24, 2024
3503704
Merge branch 'main' into enhance-command
sarahgm May 24, 2024
a121aaa
added some keywords
sarahgm May 24, 2024
1c4f24b
moooore
sarahgm May 24, 2024
2c6df92
added icons
sarahgm May 28, 2024
385d0b7
its working finally
sarahgm May 28, 2024
8f410dc
clean
sarahgm May 28, 2024
a5268bb
save
sarahgm May 28, 2024
d88e301
update
sarahgm May 28, 2024
c006585
Merge branch 'main' into enhance-command
sarahgm May 28, 2024
2f86f1f
add more links
sarahgm May 28, 2024
9df0876
better
sarahgm May 28, 2024
d5e9e9d
rename
sarahgm May 28, 2024
b2031ce
add more keywords
sarahgm May 28, 2024
de98680
copy message
sarahgm May 29, 2024
5194a33
use copy icon instead
sarahgm May 29, 2024
dc0d481
so
sarahgm May 29, 2024
a39364b
update
sarahgm May 29, 2024
54d3170
remove imports
sarahgm May 29, 2024
cc9434f
revert copy button
sarahgm May 29, 2024
9351257
asave
sarahgm May 29, 2024
3af1927
copy
sarahgm May 29, 2024
46b19eb
save
sarahgm May 31, 2024
0331cc7
Merge branch 'main' into enhance-command
sarahgm May 31, 2024
fdd9162
save?
sarahgm May 31, 2024
d75abe3
save this shit
sarahgm Jun 3, 2024
331a635
direct to slug works
sarahgm Jun 4, 2024
f15bba2
mh
sarahgm Jun 4, 2024
815ad1d
works??
sarahgm Jun 4, 2024
4a043b5
hotkeys
sarahgm Jun 4, 2024
8b7b08d
adde dthe right way of nesting the command
sarahgm Jun 6, 2024
3326e54
save
sarahgm Jun 6, 2024
ded807a
save
sarahgm Jun 6, 2024
9fbee1a
save
sarahgm Jun 7, 2024
28fde80
fast
sarahgm Jun 7, 2024
62cf2b8
save
sarahgm Jun 7, 2024
367421f
links
sarahgm Jun 7, 2024
24755ea
close sub
sarahgm Jun 7, 2024
3b6ced7
default when Open
sarahgm Jun 7, 2024
797fd79
save
sarahgm Jun 7, 2024
1461c29
works
sarahgm Jun 7, 2024
c096aca
save
sarahgm Jun 7, 2024
820dd8e
clean the mess
sarahgm Jun 7, 2024
50dbec3
hmmmmmmmmm
sarahgm Jun 7, 2024
2390715
back to before
sarahgm Jun 10, 2024
a8d62a8
save
sarahgm Jun 10, 2024
3b48292
focused Page is correct
sarahgm Jun 10, 2024
a9b1823
save
sarahgm Jun 10, 2024
6cb7f41
savvvvvee
sarahgm Jun 10, 2024
b4c4155
make it work, finally
sarahgm Jun 11, 2024
601eb9c
types
sarahgm Jun 11, 2024
1193517
fix search
sarahgm Jun 12, 2024
d339abc
move component
sarahgm Jun 12, 2024
5af825b
add comments
sarahgm Jun 12, 2024
da551f7
add comment
sarahgm Jun 12, 2024
2fa0cad
Merge branch 'main' into enhance-command
sarahgm Jun 12, 2024
bf45792
removing some whitespace
sarahgm Jun 12, 2024
d4d8237
remove unused import
sarahgm Jun 12, 2024
55afd2c
makes more sense
sarahgm Jun 12, 2024
87d070f
Create tall-coats-call.md
sarahgm Jun 12, 2024
40dfd5e
move content from file to config
sarahgm Jun 14, 2024
6fa3dae
changed some naming things
sarahgm Jun 14, 2024
37b755c
adjust text size
sarahgm Jun 14, 2024
7c5b5d6
add comment to function
sarahgm Jun 14, 2024
0a04b29
create new components out of it
sarahgm Jun 14, 2024
ce93528
add comment
sarahgm Jun 14, 2024
33af94f
Merge branch 'main' into enhance-command
sarahgm Jun 17, 2024
06bc9f6
change icon order
sarahgm Jun 17, 2024
b2915ad
added hotkey only on selected item
sarahgm Jun 17, 2024
79f3681
add some styling so it looks more like keys
sarahgm Jun 17, 2024
000e505
change naming
sarahgm Jun 19, 2024
97c466a
change namin to expand
sarahgm Jun 20, 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
5 changes: 5 additions & 0 deletions .changeset/tall-coats-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marigold/docs": patch
---

[DST-407]: enhance command menu
253 changes: 253 additions & 0 deletions docs/app/_components/CommandItems.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import { links, themeswitch } from '@/lib/config';
import { iterateTokens } from '@/lib/utils';
import { Icons, cn } from '@/ui';
import { Command, CommandGroup, useCommandState } from 'cmdk';
import {
Dispatch,
ReactNode,
RefObject,
SetStateAction,
useRef,
useState,
} from 'react';
import ReactDOMServer from 'react-dom/server';
import { useCopyToClipboard, useDebounce } from 'react-use';

import { useRouter } from 'next/navigation';

import { Inline, Split } from '@marigold/components';
import { ExternalLink } from '@marigold/icons';

import { useThemeSwitch } from '@/ui/ThemeSwitch';
import { Theme } from '@/ui/icons/Theme';

import { Hotkey } from './SiteMenu';

interface CopyItemProps {
children: ReactNode;
className?: string;
value?: string;
keywords?: string[];
onSelect?: (value: string) => void;
copyValue: string;
}

interface CommandItemProps {
classNames: {
item: string;
container: string;
section: string;
};
}

interface ChangeThemeItemProps extends CommandItemProps {
setOpen: Dispatch<SetStateAction<boolean>>;
}
interface PagesItemProps extends CommandItemProps {
name: string;
setOpen: Dispatch<SetStateAction<boolean>>;
setPages: Dispatch<SetStateAction<[]>>;
subPage: string;
items: {
title: string;
slug: string;
order: number | undefined;
headings: JSON;
}[];
}

// Components
// ---------------
export const CopyItem = ({ children, copyValue, ...props }: CopyItemProps) => {
const search = useCommandState(state => state.search);
const [isCopied, setCopy] = useState(false);
const [, setCopied] = useCopyToClipboard();
const [isReady, cancel] = useDebounce(() => setCopy(false), 2000, [isCopied]);
const copy = (value: string) => {
if (isReady()) {
cancel();
}
setCopy(true);
setCopied(value);
};

if (!search) return null;
return (
<Command.Item onSelect={() => copy(copyValue)} {...props}>
<Inline space={4} alignY="center">
{children}
<Split />
<span className="text-text-primary-muted text-xs">
{isCopied ? 'COPIED!' : 'COPY ICON'}
</span>
</Inline>
</Command.Item>
);
};

export const TokenItem = ({ classNames }: CommandItemProps) => {
const { current, themes } = useThemeSwitch();
if (!current) {
return null;
}
const tokens = iterateTokens(themes[current].colors || {});
return (
<CommandGroup heading="Colors" key="color" className={classNames.section}>
{tokens.map(([token]) => (
<CopyItem
className={classNames.item}
key={token}
value={token}
keywords={['copy']}
copyValue={token.replace('-DEFAULT', '')}
>
{token.replace('-DEFAULT', '')}
</CopyItem>
))}
</CommandGroup>
);
};

export const IconItem = ({ classNames }: CommandItemProps) => {
const ref = useRef<SVGSVGElement>();
const getIcon = (icon: keyof typeof Icons, ref: RefObject<SVGSVGElement>) => {
const Component = Icons[icon];
const iconElement = <Component ref={ref} />;
const svg = ReactDOMServer.renderToString(<Component ref={ref} />);
return { iconElement, ref, icon, svg };
};

const iconElements = Object.keys(Icons).map((icon: keyof typeof Icons) => {
return getIcon(icon, ref as any);
});
return (
<CommandGroup heading="Icons" key="icons" className={classNames.section}>
{Object.values(iconElements).map(elements => (
<CopyItem
key={elements.icon}
value={elements.icon}
keywords={['copy']}
className={classNames.item}
copyValue={elements.svg}
>
{elements.iconElement}
{elements.icon}
</CopyItem>
))}
</CommandGroup>
);
};

export const ChangeThemeItem = ({
classNames,
setOpen,
}: ChangeThemeItemProps) => {
const { updateTheme } = useThemeSwitch();
const changeTheme = (theme: string) => {
updateTheme(theme);
setOpen(false);
};
return (
<>
{themeswitch.map(({ name, items }) => (
<CommandGroup heading={name} key={name} className={classNames.section}>
{items?.map(item => (
<Command.Item
className={classNames.item}
key={item.name}
value={item.name}
keywords={['change', 'theme']}
onSelect={() => changeTheme(item.theme)}
>
<Inline space={4} alignY="center">
<span className="text-text-primary-muted text-xs">
<Theme />
</span>
{item.name}
</Inline>
</Command.Item>
))}
</CommandGroup>
))}
</>
);
};

export const ExternalLinkItem = ({ classNames }: CommandItemProps) => (
<>
{links.map(({ name, items }) => (
<CommandGroup heading={name} key={name} className={classNames.section}>
{items.map(page => (
<Command.Item
className={classNames.item}
key={page.href}
value={page.href}
onSelect={() => window.open(page.href, '_blank')}
keywords={page.keywords}
>
<Inline space={4} alignY="center">
<span className="text-text-primary-muted text-xs">
<ExternalLink />
</span>
{page.name}
</Inline>
</Command.Item>
))}
</CommandGroup>
))}
</>
);

export const PagesItem = ({
classNames,
items,
name,
setOpen,
subPage,
setPages,
}: PagesItemProps) => {
const router = useRouter();
const goto = (slug: string) => {
router.push(`/${slug}`);
setOpen(false);
setPages([]);
};
return (
<CommandGroup heading={name} key={name} className={classNames.section}>
{items.map(page => (
<>
<Command.Item
className={cn(classNames.item, 'group')}
key={page.slug}
value={page.slug}
onSelect={() => goto(page.slug)}
>
<Inline space={4} alignY="center">
{page.title}
<Split />
<Hotkey letter="D" className="hidden group-aria-selected:block" />
</Inline>
</Command.Item>
{subPage === page.slug && (
<>
{Object.values(page.headings).map(
(sub: { slug: string; text: string }) => (
<Command.Item
value={`${page.slug}${sub.slug}`}
className={cn(
'text-text-primary-muted ml-7',
classNames.item
)}
onSelect={() => goto(`${page.slug}#${sub.slug}`)}
>
{sub.text}
</Command.Item>
)
)}
</>
)}
</>
))}
</CommandGroup>
);
};
Loading
Loading