Skip to content

Commit

Permalink
Merge pull request #808 from kgar/dnd5e-3.3.x-restore-switch-clickabi…
Browse files Browse the repository at this point in the history
…lity

Resolved: Switches don't toggle when clicking their labels
  • Loading branch information
kgar authored Oct 21, 2024
2 parents 1e818aa + 3d74ebc commit 6607779
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@
assignment.item.system.sourceClass === classColumn.key}
<TidyTableHeaderCell>
<TidySwitch
value={selected}
on:change={() =>
checked={selected}
on:change={(ev) =>
setItemSourceClass(
assignment.item,
selected ? '' : classColumn.key,
ev.detail.currentTarget.checked ? classColumn.key : '',
)}
/>
</TidyTableHeaderCell>
Expand Down
8 changes: 4 additions & 4 deletions src/components/toggle/ConditionToggle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
switchOn = !condition.disabled;
}
async function handleChange(originalValue: boolean) {
async function handleChange(newValue: boolean) {
try {
await FoundryAdapter.toggleCondition($context.actor, condition);
} catch (e) {
Expand All @@ -30,7 +30,7 @@
condition,
state: switchOn,
});
switchOn = originalValue;
switchOn = !newValue;
}
}
</script>
Expand All @@ -39,8 +39,8 @@
class="flex-row small-gap tidy-condition-toggle {switchOn
? 'active'
: 'inactive'}"
bind:value={switchOn}
on:change={(ev) => handleChange(ev.detail.originalValue)}
bind:checked={switchOn}
on:change={(ev) => handleChange(ev.detail.currentTarget.checked)}
title={condition.name}
disabled={!$context.editable}
data-uuid={condition.reference}
Expand Down
18 changes: 11 additions & 7 deletions src/components/toggle/PropertyToggle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
switchOn = checked;
}
async function handleChange(originalValue: boolean) {
async function handleChange(newValue: boolean) {
try {
document.update({
[field]: !originalValue,
const result = await document.update({
[field]: newValue,
});
if (!result) {
switchOn = !newValue;
}
} catch (e) {
error('An error occurred while toggling a property', false, e);
debug('Property toggle error troubleshooting info', {
originalValue,
originalValue: !newValue,
state: switchOn,
});
switchOn = originalValue;
switchOn = !newValue;
}
}
</script>
Expand All @@ -37,8 +41,8 @@
class="flex-row small-gap tidy-property-toggle {switchOn
? 'active'
: 'inactive'}"
bind:value={switchOn}
on:change={(ev) => handleChange(ev.detail.originalValue)}
bind:checked={switchOn}
on:change={(ev) => handleChange(ev.detail.currentTarget.checked)}
{title}
{disabled}
>
Expand Down
35 changes: 13 additions & 22 deletions src/components/toggle/TidySwitch.svelte
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte';
export let value: boolean = false;
export let checked: boolean = false;
export let disabled: boolean = false;
export let thumbIconClass: string | undefined = undefined;
const switchLabelId = `switch-${foundry.utils.randomID()}-label`;
const dispatcher = createEventDispatcher<{
change: { originalValue: boolean };
change: Event & { currentTarget: HTMLInputElement };
}>();
function handleClick(
_: MouseEvent & {
currentTarget: EventTarget & HTMLElement;
},
) {
const originalValue = value;
value = !value;
dispatcher('change', { originalValue: originalValue });
}
</script>

<label
Expand All @@ -28,17 +19,17 @@
title={$$props.title ?? null}
>
<slot />
<!-- svelte-ignore a11y-interactive-supports-focus -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-missing-attribute -->
<a
role="switch"
on:click={(ev) => !disabled && handleClick(ev)}
aria-checked={value}
aria-labelledby={switchLabelId}
>
<div role="switch" aria-checked={checked} aria-labelledby={switchLabelId}>
{#if thumbIconClass}
<i class="thumb-icon {thumbIconClass}"></i>
{/if}
</a>
</div>

<input
type="checkbox"
on:change={(ev) => dispatcher('change', ev)}
{checked}
{disabled}
class="hidden"
/>
</label>
27 changes: 12 additions & 15 deletions src/components/toggle/_toggles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@
.tidy-switch {
display: flex;
align-items: center;
cursor: pointer;

--tidy-switch-scale-internal: var(--tidy-switch-scale, 1);
--tidy-switch-thumb-transform-duration-internal: var(
--tidy-switch-thumb-transform-duration,
0.3s
);

[role="switch"] {
[role='switch'] {
width: calc(2.625rem * var(--tidy-switch-scale-internal));
height: calc(1.375rem * var(--tidy-switch-scale-internal));
position: relative;
Expand Down Expand Up @@ -103,23 +104,19 @@
transition: transform var(--tidy-switch-thumb-transform-duration-internal);
}

&[aria-checked='true'] {
&:not(.disabled) {
background-color: var(--t5e-primary-accent-color);
}
&:focus-visible {
outline: 0.0625rem solid var(--t5e-primary-accent-color);
}
}

&::before,
.thumb-icon {
transform: translateX(
calc(1.25rem * var(--tidy-switch-scale-internal))
);
transition: transform
var(--tidy-switch-thumb-transform-duration-internal);
}
&:has(input:checked) [role='switch'] {
&:not(.disabled) {
background-color: var(--t5e-primary-accent-color);
}

&:focus-visible {
outline: 0.0625rem solid var(--t5e-primary-accent-color);
&::before,
.thumb-icon {
transform: translateX(calc(1.25rem * var(--tidy-switch-scale-internal)));
}
}
}
2 changes: 1 addition & 1 deletion src/sheets/actor/SheetEditModeToggle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
--tidy-switch-scale="1"
--tidy-switch-thumb-transform-duration="0.15s"
title={allowEdit ? unlockTitle : lockTitle}
value={allowEdit}
checked={allowEdit}
thumbIconClass="{allowEdit ? 'fas fa-unlock' : 'fas fa-lock'} fa-fw"
on:change={() => toggleLock()}
></TidySwitch>
Expand Down
2 changes: 1 addition & 1 deletion src/sheets/character/parts/FavoriteEffectsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<ItemTableCell baseWidth={controlsColumnWidth}>
<TidySwitch
disabled={effectContext.suppressed}
value={effectContext.toggle.value}
checked={effectContext.toggle.value}
on:change={() => toggleEffect(effectContext)}
/>
</ItemTableCell>
Expand Down
2 changes: 1 addition & 1 deletion src/sheets/shared/SheetHeaderEditModeToggle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
--tidy-switch-scale="1"
--tidy-switch-thumb-transform-duration="0.15s"
title={allowEdit ? unlockTitle : lockTitle}
value={allowEdit}
checked={allowEdit}
thumbIconClass="{allowEdit ? 'fas fa-unlock' : 'fas fa-lock'} fa-fw"
on:change={() => toggleLock()}
></TidySwitch>
Expand Down

0 comments on commit 6607779

Please sign in to comment.