Skip to content

Commit

Permalink
refactor: cleanup Toggle component (#7092)
Browse files Browse the repository at this point in the history
* refactor: cleanup `Toggle` component

* fix: remove unnecessary cursor-pointer

---------

Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
  • Loading branch information
VmMad and begonaalvarezd authored Jul 4, 2023
1 parent 747de14 commit 6915150
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
TARGET_CONTRACTS,
TRANSFER_ALLOWANCE,
} from '@core/layer-2'
import { ToggleColor } from '@ui/inputs/Toggle.svelte'
export let _onMount: (..._: any[]) => Promise<void> = async () => {}
export let disableBack = false
Expand Down Expand Up @@ -221,7 +222,7 @@
<KeyValueBox keyText={localize('general.giftStorageDeposit')}>
<Toggle
slot="value"
color="green"
color={ToggleColor.Green}
disabled={disableToggleGift}
active={giftStorageDeposit}
onClick={toggleGiftStorageDeposit}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</span>
</div>
<div class="col-end-12 h-full flex justify-center items-center justify-items-center">
<Toggle active={$isStrongholdLocked} onClick={() => {}} />
<Toggle active={$isStrongholdLocked} />
</div>
</div>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import { DrawerId, openDrawer } from '@/auxiliary/drawer'
import { sendRouter } from '@/routers'
import { activeProfile } from '@core/profile'
import { ToggleColor } from '@ui/inputs/Toggle.svelte'
export let sendTransaction: () => Promise<void>
export let storageDeposit: number
Expand Down Expand Up @@ -110,7 +111,7 @@
<KeyValueBox keyText={localize('general.giftStorageDeposit')}>
<Toggle
slot="value"
color="green"
color={ToggleColor.Green}
disabled={$newTransactionDetails.disableToggleGift}
active={$newTransactionDetails.giftStorageDeposit}
onClick={toggleGiftStorageDeposit}
Expand Down
11 changes: 6 additions & 5 deletions packages/mobile/components/filters/FilterItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
import { localize } from '@core/i18n'
import { FilterUnit } from '@core/utils/interfaces/filter'
import { ToggleColor } from '@ui/inputs/Toggle.svelte'
export let filterUnit: FilterUnit
function handleToggleClick(): void {
filterUnit.active = !filterUnit.active
}
</script>

<filter-item class="block px-5 -mx-5 border-t border-solid border-gray-200 dark:border-gray-800">
<filter-item-toggle class="flex flex-row justify-between py-4">
<Text fontWeight={FontWeight.medium} fontSize="15"
>{localize(filterUnit.labelKey ?? filterUnit.localeKey + '.label')}</Text
>
<Toggle
onClick={() => (filterUnit.active = !filterUnit.active)}
bind:active={filterUnit.active}
color="green"
/>
<Toggle onClick={handleToggleClick} bind:active={filterUnit.active} color={ToggleColor.Green} />
</filter-item-toggle>
{#if filterUnit.active}
<filter-item-type
Expand Down
66 changes: 44 additions & 22 deletions packages/shared/components/inputs/Toggle.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<script lang="ts" context="module">
export enum ToggleColor {
Green = 'green',
Blue = 'blue',
}
</script>

<script lang="ts">
export let active = false
export let classes = ''
export let color = 'blue'
export let disabled = false
export let active: boolean = false
export let color: ToggleColor = ToggleColor.Blue
export let disabled: boolean = false
export let onClick: () => void
export let onClick: (() => unknown) | undefined = undefined
function onToggleClick(): void {
if (!disabled && onClick) {
Expand All @@ -13,29 +19,45 @@
}
</script>

<button
on:click={onToggleClick}
class:active
{disabled}
type="button"
class="
toggle relative block w-10 shrink-0 h-6 rounded-full border-solid border border-transparent dark:border-gray-700
{disabled ? 'opacity-40' : 'cursor-pointer'}
{active ? `bg-${color}-500` : 'bg-gray-200 dark:bg-gray-900'}
{classes}"
>
<knob class="absolute top-1/2 transform -translate-y-1/2 left-1 h-4 w-4 rounded-full bg-white dark:bg-gray-800" />
<button on:click={onToggleClick} {disabled} type="button" class:active class={active ? `toggle-${color}` : null}>
<knob />
</button>

<style type="text/scss">
.toggle {
knob {
transition: all 0.1s cubic-bezier(0.4, 0, 0.2, 1);
<style lang="scss">
button {
@apply relative;
@apply block;
@apply w-10;
@apply shrink-0;
@apply h-6;
@apply rounded-full;
@apply border border-solid border-transparent dark:border-gray-700;
@apply bg-gray-200 dark:bg-gray-900;
&[disabled] {
@apply opacity-40;
}
&.active {
&.toggle-green {
@apply bg-green-500;
}
&.toggle-blue {
@apply bg-blue-500;
}
knob {
left: 20px;
@apply left-5;
}
}
knob {
@apply absolute;
@apply rounded-full;
@apply h-4 w-4;
@apply top-1/2 left-1;
@apply transform -translate-y-1/2;
@apply bg-white dark:bg-gray-800;
transition: all 0.1s cubic-bezier(0.4, 0, 0.2, 1);
}
}
</style>
2 changes: 1 addition & 1 deletion packages/shared/components/modals/ProfileActions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
</Text>
</div>
</div>
<Toggle active={$isStrongholdLocked} onClick={onStrongholdToggleClick} classes="cursor-pointer" />
<Toggle active={$isStrongholdLocked} onClick={onStrongholdToggleClick} />
</div>
<HR />
{:else}
Expand Down

0 comments on commit 6915150

Please sign in to comment.