Skip to content

Commit

Permalink
added dropdown component
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed Oct 15, 2021
1 parent fc92054 commit 358240e
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 87 deletions.
7 changes: 1 addition & 6 deletions devtool/src/_shared/components/Card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ export const Card: React.FC<Props> = ({
{...props}
>
{children}
{copyText && (
<CopyButton
text={copyText}
style={{ position: 'absolute', bottom: 0, right: 0 }}
/>
)}
{copyText && <CopyButton text={copyText} className={styles.copyButton} />}
</div>
);
};
6 changes: 6 additions & 0 deletions devtool/src/_shared/components/Card/style.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
height: 10rem;
}

.copyButton {
position: absolute;
bottom: 0;
right: 0;
}

14 changes: 10 additions & 4 deletions devtool/src/_shared/components/CopyButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useState, MouseEvent } from 'react';
import { Color } from '@morfeo/react';
import clsx from 'clsx';
import { Icon } from '../Icon';
Expand All @@ -7,6 +7,7 @@ import { t } from '../../utils';

type Props = {
text: string;
className?: string;
style?: React.CSSProperties;
};

Expand All @@ -24,11 +25,12 @@ function copyToClipboard(element: HTMLElement) {
return false;
}

export const CopyButton: React.FC<Props> = ({ text, style }) => {
export const CopyButton: React.FC<Props> = ({ text, style, className }) => {
const [isCopied, setIsCopied] = useState<boolean>(false);
const ref = React.useRef<HTMLSpanElement>(null);

const onClick = useCallback(() => {
const onClick = useCallback((e: MouseEvent<HTMLDivElement>) => {
e.stopPropagation();
const hasBeenCopied = copyToClipboard(ref.current as HTMLSpanElement);
if (hasBeenCopied) {
setTimeout(() => {
Expand All @@ -41,7 +43,11 @@ export const CopyButton: React.FC<Props> = ({ text, style }) => {
return (
<>
<div
className={clsx(styles.copyButton, isCopied && styles.copied)}
className={clsx(
styles.copyButton,
className,
isCopied && styles.copied,
)}
onClick={onClick}
style={style}
>
Expand Down
75 changes: 75 additions & 0 deletions devtool/src/_shared/components/DropDown/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React, { useMemo, useState, useCallback } from 'react';
import { Color } from '@morfeo/react';
import { Icon } from '../Icon';
import styles from './style.module.css';
import clsx from 'clsx';

type Option = {
label: string;
value: string;
};

type Props = {
title?: string;
value?: string;
options: Option[];
placeholder?: string;
onChange: (value: string) => void;
};

export const DropDown: React.FC<Props> = ({
title,
value,
options,
placeholder,
onChange,
}) => {
const [open, setOpen] = useState(false);

const getOnClick = useCallback(
(value: string) => {
return () => {
onChange(value);
setOpen(false);
};
},
[onChange],
);

const toggle = useCallback(() => {
setOpen(prev => !prev);
}, []);

const items = useMemo(() => {
return options.map(({ label: optionLabel, value: optionValue }) => (
<div key={optionLabel} className={styles.option}>
<h4 className="morfeo-typography-h4" onClick={getOnClick(optionValue)}>
{optionLabel}
</h4>
</div>
));
}, [options, getOnClick]);

return (
<div className={clsx(styles.container, open && styles.open)}>
{title && (
<h3 className={clsx('morfeo-typography-h3', styles.title)}>{title}</h3>
)}
<div className={styles.buttonContainer}>
<button
className={clsx('morfeo-button', styles.button)}
onClick={toggle}
>
{value || placeholder}
<Icon
name="chevron.right"
color={'textColor' as Color}
size="xs"
className={styles.toggle}
/>
</button>
<div className={styles.optionsContainer}>{items}</div>
</div>
</div>
);
};
79 changes: 79 additions & 0 deletions devtool/src/_shared/components/DropDown/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
.container {
display: flex;
min-width: 150px;
flex-direction: column;
}

.buttonContainer {
display: flex;
width: 100%;
position: relative;
}

.title {
color: var(--colors-inverted-text-color) !important;
}

.button {
width: 100%;
color: var(--colors-text-color) !important;
display: flex;
align-items: center;
justify-content: space-between;
background-color: var(--colors-background) !important;
}

.toggle {
margin-left: var(--spacings-xxs);
transition: var(--transitions-fast);
transform: rotate(90deg);
}

.open .toggle {
transform: rotate(270deg);
}

.optionsContainer {
left: 0;
right: 0;
height: 0;
width: 100%;
max-height: 0px;
/* opacity: 0; */
display: flex;
overflow: scroll;
flex-direction: column;
bottom: calc(var(--spacings-xxs) * -1);
position: absolute;
z-index: var(--z-indices-medium);
justify-content: flex-start;
align-items: flex-start;
transform: translateY(100%);
transition: var(--transitions-fast);

padding: 0;
box-shadow: var(--shadows-strong);
border-radius: var(--radii-s);
background-color: var(--colors-background) !important;
}

.option {
width: 100%;
padding-left: var(--spacings-xs);
}

.option:hover {
background-color: var(--colors-gray-lighter);
}

.open .optionsContainer {
/* opacity: 1; */
max-height: 5rem;
height: fit-content;
padding-top: var(--spacings-xs);
padding-bottom: var(--spacings-xs);
}

.option {
cursor: pointer;
}
3 changes: 2 additions & 1 deletion devtool/src/_shared/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './Link';
export * from './Icon';
export * from './Card';
export * from './Route';
export * from './Slices';
export * from './DropDown';
export * from './Accordion';
export * from './CopyButton';
export * from './Slices';
61 changes: 38 additions & 23 deletions devtool/src/_shared/css/dark-components.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,25 @@
outline: none;
font-size: var(--font-sizes-m);
font-weight: var(--font-weights-bold);
padding-top: var(--spacings-xs);
padding-left: var(--spacings-s);
padding-top: var(--spacings-xxs);
padding-left: var(--spacings-xs);
border-radius: var(--radii-xs);
padding-right: var(--spacings-s);
padding-bottom: var(--spacings-xs);
padding-right: var(--spacings-xs);
padding-bottom: var(--spacings-xxs);
background-color: var(--colors-primary);
}
[data-morfeo-theme="dark"] .morfeo-button-round {
color: var(--colors-inverted-text-color);
border: var(--border-widths-none) var(--border-styles-none);
cursor: pointer;
outline: none;
font-size: var(--font-sizes-m);
font-weight: var(--font-weights-bold);
padding-top: var(--spacings-none);
padding-left: var(--spacings-none);
border-radius: var(--radii-round);
padding-right: var(--spacings-none);
padding-bottom: var(--spacings-none);
background-color: var(--colors-primary);
}
[data-morfeo-theme="dark"] .morfeo-button-side {
Expand Down Expand Up @@ -76,11 +90,11 @@
outline: none;
font-size: var(--font-sizes-m);
font-weight: var(--font-weights-bold);
padding-top: var(--spacings-xs);
padding-left: var(--spacings-s);
padding-top: var(--spacings-xxs);
padding-left: var(--spacings-xs);
border-radius: var(--radii-xs);
padding-right: var(--spacings-s);
padding-bottom: var(--spacings-xs);
padding-right: var(--spacings-xs);
padding-bottom: var(--spacings-xxs);
background-color: var(--colors-light);
}
[data-morfeo-theme="dark"] .morfeo-button-success {
Expand All @@ -90,74 +104,75 @@
outline: none;
font-size: var(--font-sizes-m);
font-weight: var(--font-weights-bold);
padding-top: var(--spacings-xs);
padding-left: var(--spacings-s);
padding-top: var(--spacings-xxs);
padding-left: var(--spacings-xs);
border-radius: var(--radii-xs);
padding-right: var(--spacings-s);
padding-bottom: var(--spacings-xs);
padding-right: var(--spacings-xs);
padding-bottom: var(--spacings-xxs);
background-color: var(--colors-success);
}
[data-morfeo-theme="dark"] .morfeo-typography {
color: var(--colors-text-color);
font-family: var(--fonts-regular);
}
[data-morfeo-theme="dark"] .morfeo-typography-hero {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-l);
font-family: var(--fonts-regular);
font-weight: var(--font-weights-bold);
line-height: var(--line-heights-heading);
}
[data-morfeo-theme="dark"] .morfeo-typography-h1 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-xl);
font-family: var(--fonts-regular);
font-weight: var(--font-weights-bold);
line-height: var(--line-heights-heading);
letter-spacing: var(--letter-spacings-heading);
}
[data-morfeo-theme="dark"] .morfeo-typography-h2 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-m);
font-family: var(--fonts-regular);
font-weight: var(--font-weights-bold);
line-height: var(--line-heights-heading);
}
[data-morfeo-theme="dark"] .morfeo-typography-h3 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-s);
font-family: var(--fonts-regular);
font-weight: var(--font-weights-bold);
line-height: var(--line-heights-heading);
}
[data-morfeo-theme="dark"] .morfeo-typography-h4 {
color: var(--colors-dark);
font-size: var(--font-sizes-xs);
color: var(--colors-text-color);
font-size: var(--font-sizes-s);
font-family: var(--fonts-regular);
font-weight: var(--font-weights-bold);
line-height: var(--line-heights-heading);
}
[data-morfeo-theme="dark"] .morfeo-typography-p1 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-m);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
letter-spacing: var(--letter-spacings-body);
}
[data-morfeo-theme="dark"] .morfeo-typography-p2 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-s);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
letter-spacing: var(--letter-spacings-body);
}
[data-morfeo-theme="dark"] .morfeo-typography-p3 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-xs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
}
[data-morfeo-theme="dark"] .morfeo-typography-p4 {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-xxs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
Expand All @@ -173,7 +188,7 @@
color: var(--colors-primary-lightest);
}
[data-morfeo-theme="dark"] .morfeo-typography-caption {
color: var(--colors-dark);
color: var(--colors-text-color);
font-size: var(--font-sizes-xxs);
font-style: italic;
font-family: var(--fonts-regular);
Expand Down
Loading

0 comments on commit 358240e

Please sign in to comment.