Skip to content

Commit 82ebb0c

Browse files
committed
use dropdown for lang
1 parent 9d234d6 commit 82ebb0c

File tree

5 files changed

+25
-83
lines changed

5 files changed

+25
-83
lines changed

apps/site/components/withNavBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const WithNavBar: FC = () => {
7070
onChange={changeLanguage}
7171
availableLanguages={availableLocales}
7272
currentLanguage={locale}
73-
ariaLabel={t('components.common.languageDropdown.label')}
73+
aria-label={t('components.common.languageDropdown.label')}
7474
/>
7575

7676
<Link

packages/ui-components/Common/Dropdown/index.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ type Meta = MetaObj<typeof Dropdown>;
88
// Basic example with default anchor links
99
export const Default: Story = {
1010
args: {
11-
label: 'History',
1211
values: [
1312
{ href: '#item1', children: 'Item 1' },
1413
{ href: '#item2', children: 'Item 2' },

packages/ui-components/Common/Dropdown/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { LinkLike } from '#ui/types.js';
66
import styles from './index.module.css';
77

88
type DropdownProps = ComponentProps<'div'> & {
9-
label: string;
109
values: Array<HTMLProps<HTMLAnchorElement | HTMLDivElement>>;
1110
as?: LinkLike;
1211
};
@@ -35,20 +34,20 @@ const Dropdown: FC<PropsWithChildren<DropdownProps>> = ({
3534
return (
3635
<As
3736
key={index}
38-
className={styles.dropdownItem}
3937
role="menuitem"
4038
tabIndex={0}
4139
{...(value as ComponentProps<LinkLike>)}
40+
className={classNames(styles.dropdownItem, value.className)}
4241
/>
4342
);
4443
} else {
4544
return (
4645
<div
4746
key={index}
48-
className={styles.dropdownItem}
4947
role="menuitem"
5048
tabIndex={0}
5149
{...(value as HTMLProps<HTMLDivElement>)}
50+
className={classNames(styles.dropdownItem, value.className)}
5251
/>
5352
);
5453
}
Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,12 @@
11
@reference "../../styles/index.css";
22

33
.languageDropdown {
4-
@apply h-9
5-
w-9
6-
rounded-md
7-
p-2
8-
text-neutral-700
9-
motion-safe:transition-colors
10-
dark:text-neutral-300;
11-
12-
&:hover {
13-
@apply bg-neutral-100
14-
dark:bg-neutral-900;
4+
summary {
5+
@apply border-0;
156
}
16-
}
177

18-
.dropDownContent {
19-
@apply max-h-80
20-
w-48
21-
overflow-hidden
22-
rounded-sm
23-
border
24-
border-neutral-200
25-
bg-white
26-
shadow-lg
27-
dark:border-neutral-900
28-
dark:bg-neutral-950;
29-
30-
> div {
31-
@apply max-h-80
32-
w-48
33-
overflow-y-auto;
8+
.selected {
9+
@apply bg-green-600
10+
text-white;
3411
}
3512
}
36-
37-
.dropDownItem {
38-
@apply outline-hidden
39-
cursor-pointer
40-
px-2.5
41-
py-1.5
42-
text-sm
43-
font-medium
44-
text-neutral-800
45-
data-[highlighted]:bg-green-600
46-
data-[highlighted]:text-white
47-
dark:text-white;
48-
}
49-
50-
.currentDropDown {
51-
@apply bg-green-600
52-
text-white;
53-
}

packages/ui-components/Common/LanguageDropDown/index.tsx

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { LanguageIcon } from '@heroicons/react/24/outline';
2-
import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
32
import classNames from 'classnames';
43
import type { FC } from 'react';
54

5+
import Dropdown from '#ui/Common/Dropdown';
66
import type { SimpleLocaleConfig } from '#ui/types';
77

88
import styles from './index.module.css';
@@ -11,45 +11,30 @@ type LanguageDropDownProps = {
1111
onChange?: (newLocale: SimpleLocaleConfig) => void;
1212
currentLanguage: string;
1313
availableLanguages: Array<SimpleLocaleConfig>;
14-
ariaLabel: string;
1514
};
1615

1716
const LanguageDropdown: FC<LanguageDropDownProps> = ({
1817
onChange = () => {},
1918
currentLanguage,
2019
availableLanguages,
21-
ariaLabel,
20+
...props
2221
}) => {
2322
return (
24-
<DropdownMenu.Root>
25-
<DropdownMenu.Trigger asChild>
26-
<button className={styles.languageDropdown} aria-label={ariaLabel}>
27-
<LanguageIcon height="20" />
28-
</button>
29-
</DropdownMenu.Trigger>
30-
31-
<DropdownMenu.Portal>
32-
<DropdownMenu.Content
33-
align="start"
34-
className={styles.dropDownContent}
35-
sideOffset={5}
36-
>
37-
<div>
38-
{availableLanguages.map(({ name, code, localName }) => (
39-
<DropdownMenu.Item
40-
key={code}
41-
onClick={() => onChange({ name, code, localName })}
42-
className={classNames(styles.dropDownItem, {
43-
[styles.currentDropDown]: code === currentLanguage,
44-
})}
45-
>
46-
{localName}
47-
</DropdownMenu.Item>
48-
))}
49-
</div>
50-
</DropdownMenu.Content>
51-
</DropdownMenu.Portal>
52-
</DropdownMenu.Root>
23+
<Dropdown
24+
values={availableLanguages.map(({ name, code, localName }) => ({
25+
onClick: () => onChange({ name, code, localName }),
26+
children: localName,
27+
key: code,
28+
className: classNames({
29+
'cursor-pointer': true,
30+
[styles.selected]: currentLanguage === code,
31+
}),
32+
}))}
33+
className={styles.languageDropdown}
34+
{...props}
35+
>
36+
<LanguageIcon height="20" />
37+
</Dropdown>
5338
);
5439
};
5540

0 commit comments

Comments
 (0)