-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): Button with popup (#639)
* feat(frontend): Add ButtonPopup component
- Loading branch information
Showing
16 changed files
with
611 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
frontend/src/lib/components/common/button/ButtonPopup.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<script lang="ts"> | ||
import { faChevronDown } from '@fortawesome/free-solid-svg-icons' | ||
import { setContext } from 'svelte' | ||
import Icon from 'svelte-awesome' | ||
import { Button, ButtonType, Popup } from '..' | ||
export let size: ButtonType.Size = 'md' | ||
export let color: ButtonType.Color = 'blue' | ||
export let variant: ButtonType.Variant = 'contained' | ||
export let mainClasses: string = '' | ||
export let toggleClasses: string = '' | ||
export let disabled: boolean = false | ||
export let href: string | undefined = undefined | ||
export let target: ButtonType.Target = '_self' | ||
export let startIcon: ButtonType.Icon | undefined = undefined | ||
export let endIcon: ButtonType.Icon | undefined = undefined | ||
let ref: ButtonType.Element | ||
setContext<ButtonType.ItemProps>(ButtonType.ItemContextKey, { size, color }) | ||
$: separator = color === 'red' || color === 'blue' ? 'border-gray-200' : 'border-gray-400' | ||
$: commonProps = { | ||
size, | ||
color, | ||
variant, | ||
disabled | ||
} | ||
</script> | ||
|
||
<div class="flex justy-start items-center"> | ||
{#if $$slots.main} | ||
<Button | ||
{...commonProps} | ||
{href} | ||
{target} | ||
{startIcon} | ||
{endIcon} | ||
btnClasses="!rounded-r-none !border-r-0 {mainClasses}" | ||
on:click | ||
> | ||
<slot name="main" /> | ||
</Button> | ||
{/if} | ||
<span class={$$slots.main && variant === 'contained' ? 'border-l ' + separator : ''}> | ||
<Button | ||
bind:element={ref} | ||
{...commonProps} | ||
btnClasses="{$$slots.main ? '!rounded-l-none' : ''} {toggleClasses}" | ||
> | ||
<slot name="toggle"> | ||
<!-- Invisible, but needed to match the height of the 'main' button --> | ||
<span class="!opacity-0 !w-0">A</span> | ||
<Icon data={faChevronDown} scale={ButtonType.IconScale[size]} /> | ||
</slot> | ||
</Button> | ||
</span> | ||
</div> | ||
{#if ref} | ||
<Popup | ||
{ref} | ||
options={{ | ||
placement: $$slots.main ? 'bottom-end' : 'bottom', | ||
strategy: 'absolute', | ||
modifiers: [{ name: 'offset', options: { offset: [0, 0] } }] | ||
}} | ||
> | ||
<ul class="bg-white rounded-t border pt-1 pb-2 max-h-40 overflow-auto"> | ||
<slot /> | ||
</ul> | ||
</Popup> | ||
{/if} |
52 changes: 52 additions & 0 deletions
52
frontend/src/lib/components/common/button/ButtonPopupItem.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<script lang="ts"> | ||
import { getContext } from 'svelte' | ||
import { Button, ButtonType } from '..' | ||
import { classNames } from '../../../utils' | ||
export let btnClasses: string = '' | ||
export let disabled: boolean = false | ||
export let href: string | undefined = undefined | ||
export let target: ButtonType.Target = '_self' | ||
export let iconOnly: boolean = false | ||
export let startIcon: ButtonType.Icon | undefined = undefined | ||
export let endIcon: ButtonType.Icon | undefined = undefined | ||
const props = getContext<ButtonType.ItemProps | undefined>(ButtonType.ItemContextKey) | ||
const iconWidthClass: Record<ButtonType.Size, string> = { | ||
xs: '!w-[12px]', | ||
sm: '!w-[14px]', | ||
md: '!w-[16px]', | ||
lg: '!w-[18px]', | ||
xl: '!w-[20px]' | ||
} | ||
const getWidthClass = () => (props?.size ? iconWidthClass[props.size] : undefined) | ||
$: buttonProps = { | ||
...props, | ||
variant: <ButtonType.Variant>'border', | ||
btnClasses: classNames(btnClasses, '!justify-start !border-0 !rounded-none !w-full'), | ||
disabled, | ||
href, | ||
target, | ||
iconOnly, | ||
startIcon: startIcon | ||
? { | ||
icon: startIcon.icon, | ||
classes: classNames(startIcon?.classes, getWidthClass()) | ||
} | ||
: undefined, | ||
endIcon: endIcon | ||
? { | ||
icon: endIcon.icon, | ||
classes: classNames(endIcon?.classes, getWidthClass()) | ||
} | ||
: undefined | ||
} | ||
</script> | ||
|
||
<li class="mt-1"> | ||
<Button {...buttonProps} on:click> | ||
<slot /> | ||
</Button> | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,44 @@ | ||
export namespace Button { | ||
export namespace ButtonType { | ||
export type Size = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | ||
export type Color = 'blue' | 'red' | 'dark' | 'light' | ||
export type Variant = 'contained' | 'border' | ||
export type Target = '_self' | '_blank' | ||
export type Element = HTMLButtonElement | HTMLAnchorElement | ||
export interface Icon { | ||
icon: any | ||
classes?: string | ||
} | ||
|
||
export const FontSizeClasses: Record<ButtonType.Size, string> = { | ||
xs: 'text-xs', | ||
sm: 'text-sm', | ||
md: 'text-md', | ||
lg: 'text-lg', | ||
xl: 'text-xl' | ||
} as const | ||
|
||
export const SpacingClasses: Record<ButtonType.Size, string> = { | ||
xs: 'px-3 py-1.5', | ||
sm: 'px-3 py-1.5', | ||
md: 'px-4 py-2', | ||
lg: 'px-4 py-2', | ||
xl: 'px-4 py-2' | ||
} as const | ||
|
||
export const IconScale: Record<ButtonType.Size, number> = { | ||
xs: 0.7, | ||
sm: 0.8, | ||
md: 1, | ||
lg: 1.1, | ||
xl: 1.2 | ||
} as const | ||
|
||
// ButtonPopup types | ||
|
||
export const ItemContextKey = 'popupItemProps' as const | ||
|
||
export interface ItemProps { | ||
size: Size | ||
color: Color | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<kbd | ||
class="mx-1 px-2 py-1.5 text-xs font-semibold text-gray-800 | ||
bg-gray-100 border border-gray-200 rounded-lg | ||
dark:bg-gray-600 dark:text-gray-100 dark:border-gray-500 {$$props.class}" | ||
> | ||
<slot /> | ||
</kbd> |
Oops, something went wrong.