2025-11-30 21:54:10 +00:00
|
|
|
import { cssManager, colors } from './shared.js';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
DeesElement,
|
|
|
|
|
customElement,
|
|
|
|
|
html,
|
|
|
|
|
css,
|
|
|
|
|
type TemplateResult,
|
|
|
|
|
property,
|
|
|
|
|
} from '@design.estate/dees-element';
|
|
|
|
|
|
2025-01-14 03:08:00 +01:00
|
|
|
import { delayFor } from '@push.rocks/smartdelay';
|
|
|
|
|
|
2025-01-13 22:20:19 +01:00
|
|
|
@customElement('consentsoftware-toggle')
|
2025-11-30 21:54:10 +00:00
|
|
|
export class ConsentsoftwareToggle extends DeesElement {
|
2025-01-13 22:20:19 +01:00
|
|
|
@property({ type: Boolean })
|
2025-01-17 17:45:33 +01:00
|
|
|
public accessor required = false;
|
2025-01-13 22:20:19 +01:00
|
|
|
|
|
|
|
|
@property({ type: Boolean, reflect: true })
|
2025-01-17 17:45:33 +01:00
|
|
|
public accessor selected = false;
|
2025-01-13 22:20:19 +01:00
|
|
|
|
2025-01-16 01:23:13 +01:00
|
|
|
/**
|
2025-11-30 21:43:36 +00:00
|
|
|
* Knob position tracking (0 = off, maxTravel = on)
|
|
|
|
|
* This is the travel distance, not absolute left position.
|
|
|
|
|
* Actual left = padding + currentX
|
2025-01-16 01:23:13 +01:00
|
|
|
*/
|
|
|
|
|
private currentX = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drag state
|
|
|
|
|
*/
|
|
|
|
|
private isDragging = false;
|
|
|
|
|
private hasDragged = false;
|
2025-11-30 21:43:36 +00:00
|
|
|
private startX = 0;
|
|
|
|
|
|
2025-11-30 21:54:10 +00:00
|
|
|
// Toggle dimensions (with border-box, 1px border reduces inner by 2px)
|
|
|
|
|
private readonly trackWidth = 36; // outer width
|
|
|
|
|
private readonly trackHeight = 20; // outer height
|
2025-11-30 21:43:36 +00:00
|
|
|
private readonly knobSize = 14;
|
2025-11-30 21:54:10 +00:00
|
|
|
private readonly padding = 2; // padding from inner edge to knob
|
|
|
|
|
private readonly maxTravel = 16; // (36 - 2) - 14 - (2 * 2) = 34 - 14 - 4 = 16
|
|
|
|
|
|
|
|
|
|
public static styles = [
|
|
|
|
|
cssManager.defaultStyles,
|
|
|
|
|
css`
|
|
|
|
|
:host {
|
|
|
|
|
display: block;
|
|
|
|
|
position: relative;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.label {
|
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
font-size: 0.8em;
|
|
|
|
|
font-weight: 500;
|
|
|
|
|
color: ${cssManager.bdTheme(colors.light.mutedForeground, colors.dark.mutedForeground)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggle {
|
|
|
|
|
user-select: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggleKnobArea {
|
|
|
|
|
position: relative;
|
|
|
|
|
margin: auto;
|
|
|
|
|
height: 20px;
|
|
|
|
|
width: 36px;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
background: ${cssManager.bdTheme(colors.light.input, colors.dark.input)};
|
|
|
|
|
border: 1px solid ${cssManager.bdTheme(colors.light.border, colors.dark.border)};
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
transition: all 0.15s ease;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggleKnobArea:hover {
|
|
|
|
|
border-color: ${cssManager.bdTheme(colors.light.ring, colors.dark.ring)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:host([selected]) .toggleKnobArea {
|
|
|
|
|
background: ${cssManager.bdTheme(colors.light.primary, colors.dark.primary)};
|
|
|
|
|
border-color: ${cssManager.bdTheme(colors.light.primary, colors.dark.primary)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggleKnobMover {
|
|
|
|
|
position: relative;
|
|
|
|
|
height: 100%;
|
|
|
|
|
width: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggleKnobInner {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 2px;
|
|
|
|
|
width: 14px;
|
|
|
|
|
height: 14px;
|
|
|
|
|
border-radius: 7px;
|
|
|
|
|
background: ${cssManager.bdTheme(colors.light.mutedForeground, colors.dark.mutedForeground)};
|
|
|
|
|
transition: left 0.15s ease, background 0.15s ease;
|
|
|
|
|
touch-action: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.toggleKnobInner.dragging {
|
|
|
|
|
transition: background 0.15s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:host([selected]) .toggleKnobInner {
|
|
|
|
|
background: ${cssManager.bdTheme(colors.light.primaryForeground, colors.dark.primaryForeground)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:host([required]) .toggleKnobArea {
|
|
|
|
|
cursor: not-allowed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:host([required][selected]) .toggleKnobArea {
|
|
|
|
|
background: ${cssManager.bdTheme(colors.light.ring, colors.dark.ring)};
|
|
|
|
|
border-color: ${cssManager.bdTheme(colors.light.ring, colors.dark.ring)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
:host([required][selected]) .toggleKnobInner {
|
|
|
|
|
background: ${cssManager.bdTheme(colors.light.muted, colors.dark.muted)};
|
|
|
|
|
}
|
|
|
|
|
`,
|
|
|
|
|
];
|
2025-01-13 22:20:19 +01:00
|
|
|
|
|
|
|
|
public render(): TemplateResult {
|
|
|
|
|
return html`
|
|
|
|
|
<div class="label">${this.getText()}</div>
|
2025-01-16 01:23:13 +01:00
|
|
|
<!-- A user can click anywhere in this toggle area. -->
|
2025-01-13 22:20:19 +01:00
|
|
|
<div class="toggle" @click=${this.handleClick}>
|
|
|
|
|
<div class="toggleKnobArea">
|
|
|
|
|
<div class="toggleKnobMover">
|
2025-01-16 01:23:13 +01:00
|
|
|
<!-- The knob itself, with pointer events for dragging. -->
|
|
|
|
|
<div
|
|
|
|
|
class="toggleKnobInner"
|
2025-11-30 21:43:36 +00:00
|
|
|
style="left: ${this.padding + this.currentX}px;"
|
2025-01-16 01:23:13 +01:00
|
|
|
@pointerdown=${this.onPointerDown}
|
|
|
|
|
@pointermove=${this.onPointerMove}
|
|
|
|
|
@pointerup=${this.onPointerUp}
|
|
|
|
|
@pointercancel=${this.onPointerUp}
|
|
|
|
|
></div>
|
2025-01-13 22:20:19 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 01:23:13 +01:00
|
|
|
/**
|
|
|
|
|
* If required = true on first render, auto-select and set the knob to the right.
|
|
|
|
|
*/
|
2025-11-30 21:54:10 +00:00
|
|
|
public async firstUpdated(_changedProperties: Map<PropertyKey, unknown>) {
|
|
|
|
|
await super.firstUpdated(_changedProperties);
|
2025-01-14 03:08:00 +01:00
|
|
|
if (this.required) {
|
|
|
|
|
this.selected = true;
|
2025-11-30 21:43:36 +00:00
|
|
|
this.currentX = this.maxTravel;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
|
|
|
|
} else {
|
2025-11-30 21:43:36 +00:00
|
|
|
this.currentX = this.selected ? this.maxTravel : 0;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
2025-01-14 03:08:00 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 01:23:13 +01:00
|
|
|
/**
|
|
|
|
|
* CLICK HANDLER
|
|
|
|
|
*/
|
|
|
|
|
public async handleClick(event: MouseEvent) {
|
|
|
|
|
if (this.isDragging || this.hasDragged) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 03:08:00 +01:00
|
|
|
if (this.required) {
|
2025-11-30 21:43:36 +00:00
|
|
|
// Small bounce animation for required toggles
|
|
|
|
|
this.currentX = this.maxTravel;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
|
|
|
|
await new Promise((r) => setTimeout(r, 10));
|
2025-11-30 21:43:36 +00:00
|
|
|
this.currentX = this.maxTravel - 3;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
2025-11-30 21:43:36 +00:00
|
|
|
await delayFor(150);
|
|
|
|
|
this.currentX = this.maxTravel;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
2025-01-14 03:08:00 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2025-01-16 01:23:13 +01:00
|
|
|
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
2025-01-13 22:20:19 +01:00
|
|
|
this.selected = !this.selected;
|
2025-11-30 21:43:36 +00:00
|
|
|
this.currentX = this.selected ? this.maxTravel : 0;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
|
|
|
|
|
2025-01-13 22:20:19 +01:00
|
|
|
this.dispatchEvent(new CustomEvent('toggle', { detail: { selected: this.selected } }));
|
2025-01-16 01:23:13 +01:00
|
|
|
delayFor(0).then(() => {
|
|
|
|
|
this.hasDragged = false;
|
|
|
|
|
});
|
2025-01-14 03:08:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-16 01:23:13 +01:00
|
|
|
/**
|
|
|
|
|
* DRAG HANDLERS (pointer events)
|
|
|
|
|
*/
|
|
|
|
|
private onPointerDown(event: PointerEvent) {
|
|
|
|
|
if (this.required) {
|
|
|
|
|
// If "required", disallow manual dragging
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start dragging
|
|
|
|
|
this.isDragging = true;
|
2025-11-30 21:54:10 +00:00
|
|
|
// The difference between the pointer's X and the knob's current position
|
2025-01-16 01:23:13 +01:00
|
|
|
this.startX = event.clientX - this.currentX;
|
|
|
|
|
|
|
|
|
|
// capture pointer so we keep receiving pointermove/pointerup
|
|
|
|
|
(event.target as HTMLElement).setPointerCapture(event.pointerId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onPointerMove(event: PointerEvent) {
|
|
|
|
|
if (!this.isDragging) return;
|
|
|
|
|
const newX = event.clientX - this.startX;
|
|
|
|
|
this.hasDragged = true;
|
|
|
|
|
const toggleKnobInner: HTMLDivElement = this.shadowRoot.querySelector('.toggleKnobInner');
|
|
|
|
|
toggleKnobInner.classList.add('dragging');
|
|
|
|
|
|
2025-11-30 21:43:36 +00:00
|
|
|
// Clamp to valid travel range (0 to maxTravel)
|
|
|
|
|
this.currentX = Math.max(0, Math.min(newX, this.maxTravel));
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onPointerUp(event: PointerEvent) {
|
|
|
|
|
if (!this.isDragging) return;
|
|
|
|
|
(event.target as HTMLElement).releasePointerCapture(event.pointerId);
|
|
|
|
|
this.isDragging = false;
|
|
|
|
|
|
2025-11-30 21:43:36 +00:00
|
|
|
// If we didn't truly drag, pointerup does nothing; click handler handles toggling.
|
2025-01-16 01:23:13 +01:00
|
|
|
if (!this.hasDragged) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const toggleKnobInner: HTMLDivElement = this.shadowRoot.querySelector('.toggleKnobInner');
|
|
|
|
|
toggleKnobInner.classList.remove('dragging');
|
|
|
|
|
|
2025-11-30 21:43:36 +00:00
|
|
|
// Real drag => decide final side based on midpoint
|
|
|
|
|
const midpoint = this.maxTravel / 2;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.selected = this.currentX > midpoint;
|
2025-11-30 21:43:36 +00:00
|
|
|
this.currentX = this.selected ? this.maxTravel : 0; // snap to edge
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
|
|
|
|
|
|
|
|
|
// Dispatch toggle event
|
|
|
|
|
this.dispatchEvent(new CustomEvent('toggle', { detail: { selected: this.selected } }));
|
2025-01-16 02:59:10 +01:00
|
|
|
delayFor(0).then(() => {
|
|
|
|
|
this.hasDragged = false;
|
|
|
|
|
});
|
2025-01-16 01:23:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If external code sets `selected = true/false`, we also sync the knob position
|
|
|
|
|
*/
|
|
|
|
|
protected updated(changedProperties: Map<PropertyKey, unknown>): void {
|
|
|
|
|
if (
|
|
|
|
|
changedProperties.has('selected') &&
|
|
|
|
|
!this.isDragging &&
|
|
|
|
|
!this.hasDragged &&
|
|
|
|
|
!this.required
|
|
|
|
|
) {
|
2025-11-30 21:43:36 +00:00
|
|
|
this.currentX = this.selected ? this.maxTravel : 0;
|
2025-01-16 01:23:13 +01:00
|
|
|
this.requestUpdate();
|
2025-01-13 22:20:19 +01:00
|
|
|
}
|
2025-01-16 01:23:13 +01:00
|
|
|
super.updated(changedProperties);
|
2025-01-13 22:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-16 01:23:13 +01:00
|
|
|
public getText(): string | null {
|
2025-01-13 22:20:19 +01:00
|
|
|
return this.textContent;
|
|
|
|
|
}
|
|
|
|
|
}
|