2024-01-10 05:11:55 +01:00
|
|
|
import {
|
|
|
|
customElement,
|
|
|
|
type TemplateResult,
|
|
|
|
html,
|
|
|
|
property,
|
|
|
|
css,
|
|
|
|
cssManager,
|
|
|
|
} from '@design.estate/dees-element';
|
2025-06-19 11:39:16 +00:00
|
|
|
import { DeesInputBase } from './dees-input-base.js';
|
|
|
|
|
|
|
|
import * as colors from './00colors.js'
|
2024-01-10 05:11:55 +01:00
|
|
|
|
2025-06-26 23:34:11 +00:00
|
|
|
import { demoFunc } from './dees-input-multitoggle.demo.js';
|
2024-01-10 05:11:55 +01:00
|
|
|
|
2024-12-17 19:44:47 +01:00
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'dees-input-multitoggle': DeesInputMultitoggle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
@customElement('dees-input-multitoggle')
|
2025-06-19 11:39:16 +00:00
|
|
|
export class DeesInputMultitoggle extends DeesInputBase<DeesInputMultitoggle> {
|
2024-01-10 05:11:55 +01:00
|
|
|
public static demo = demoFunc;
|
|
|
|
|
2024-01-21 01:45:35 +01:00
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
@property()
|
|
|
|
type: 'boolean' | 'multi' | 'single' = 'multi';
|
|
|
|
|
|
|
|
@property()
|
|
|
|
booleanTrueName: string = 'true';
|
|
|
|
|
|
|
|
@property()
|
|
|
|
booleanFalseName: string = 'false';
|
|
|
|
|
|
|
|
@property({
|
|
|
|
type: Array,
|
|
|
|
})
|
|
|
|
options: string[] = [];
|
|
|
|
|
|
|
|
@property()
|
|
|
|
selectedOption: string = '';
|
|
|
|
|
2025-06-19 11:39:16 +00:00
|
|
|
@property({ type: Boolean })
|
2024-01-10 05:11:55 +01:00
|
|
|
boolValue: boolean = false;
|
|
|
|
|
2025-06-19 11:39:16 +00:00
|
|
|
// Add value property for form compatibility
|
|
|
|
public get value(): string | boolean {
|
|
|
|
if (this.type === 'boolean') {
|
|
|
|
return this.selectedOption === this.booleanTrueName;
|
|
|
|
}
|
|
|
|
return this.selectedOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set value(val: string | boolean) {
|
|
|
|
if (this.type === 'boolean' && typeof val === 'boolean') {
|
|
|
|
this.selectedOption = val ? this.booleanTrueName : this.booleanFalseName;
|
|
|
|
} else {
|
|
|
|
this.selectedOption = val as string;
|
|
|
|
}
|
2025-06-27 21:16:52 +00:00
|
|
|
this.requestUpdate();
|
2025-06-19 11:39:16 +00:00
|
|
|
// Defer indicator update to next frame if component not yet updated
|
|
|
|
if (this.hasUpdated) {
|
2025-06-27 21:16:52 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.setIndicator();
|
|
|
|
});
|
2025-06-19 11:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
public static styles = [
|
2025-06-19 11:39:16 +00:00
|
|
|
...DeesInputBase.baseStyles,
|
2024-01-10 05:11:55 +01:00
|
|
|
cssManager.defaultStyles,
|
|
|
|
css`
|
|
|
|
:host {
|
2025-06-27 21:16:52 +00:00
|
|
|
color: ${cssManager.bdTheme('#09090b', '#fafafa')};
|
2024-01-10 05:11:55 +01:00
|
|
|
user-select: none;
|
|
|
|
}
|
2024-01-18 02:08:19 +01:00
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
.selections {
|
|
|
|
position: relative;
|
2025-06-27 21:16:52 +00:00
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
background: ${cssManager.bdTheme('#ffffff', '#18181b')};
|
|
|
|
border: 1px solid ${cssManager.bdTheme('#e5e7eb', '#27272a')};
|
|
|
|
padding: 4px;
|
|
|
|
border-radius: 8px;
|
|
|
|
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
2024-01-10 05:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.option {
|
|
|
|
position: relative;
|
2025-06-27 21:16:52 +00:00
|
|
|
padding: 8px 20px;
|
|
|
|
border-radius: 6px;
|
|
|
|
cursor: pointer;
|
|
|
|
white-space: nowrap;
|
|
|
|
transition: color 0.2s ease;
|
2024-01-18 02:08:19 +01:00
|
|
|
font-size: 14px;
|
2025-06-27 21:16:52 +00:00
|
|
|
font-weight: 500;
|
|
|
|
color: ${cssManager.bdTheme('#71717a', '#71717a')};
|
|
|
|
line-height: 1;
|
|
|
|
z-index: 2;
|
2024-01-11 21:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.option:hover {
|
2025-06-27 21:16:52 +00:00
|
|
|
color: ${cssManager.bdTheme('#18181b', '#e4e4e7')};
|
2024-01-10 05:11:55 +01:00
|
|
|
}
|
|
|
|
|
2024-01-18 02:08:19 +01:00
|
|
|
.option.selected {
|
2025-06-27 21:16:52 +00:00
|
|
|
color: ${cssManager.bdTheme('#3b82f6', '#60a5fa')};
|
2024-01-18 02:08:19 +01:00
|
|
|
}
|
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
.indicator {
|
|
|
|
opacity: 0;
|
|
|
|
position: absolute;
|
2025-06-27 21:16:52 +00:00
|
|
|
height: calc(100% - 8px);
|
|
|
|
top: 4px;
|
|
|
|
border-radius: 6px;
|
|
|
|
background: ${cssManager.bdTheme('rgba(59, 130, 246, 0.15)', 'rgba(59, 130, 246, 0.15)')};
|
|
|
|
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
|
|
|
z-index: 1;
|
2025-06-19 11:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.indicator.no-transition {
|
|
|
|
transition: none;
|
2024-01-10 05:11:55 +01:00
|
|
|
}
|
2025-06-27 21:16:52 +00:00
|
|
|
|
|
|
|
:host([disabled]) .selections {
|
|
|
|
opacity: 0.5;
|
|
|
|
cursor: not-allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
:host([disabled]) .option {
|
|
|
|
cursor: not-allowed;
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
:host([disabled]) .indicator {
|
|
|
|
background: ${cssManager.bdTheme('rgba(113, 113, 122, 0.15)', 'rgba(113, 113, 122, 0.15)')};
|
|
|
|
}
|
2024-01-10 05:11:55 +01:00
|
|
|
`,
|
|
|
|
];
|
|
|
|
|
|
|
|
public render(): TemplateResult {
|
|
|
|
return html`
|
2025-06-19 11:39:16 +00:00
|
|
|
<div class="input-wrapper">
|
|
|
|
<dees-label .label=${this.label} .description=${this.description}></dees-label>
|
|
|
|
<div class="mainbox">
|
|
|
|
<div class="selections">
|
2024-01-10 05:11:55 +01:00
|
|
|
<div class="indicator"></div>
|
2024-01-18 02:08:19 +01:00
|
|
|
${this.options.map(
|
|
|
|
(option) =>
|
|
|
|
html`<div class="option ${option === this.selectedOption ? 'selected': ''}" @click=${() => this.handleSelection(option)}>
|
|
|
|
${option}
|
|
|
|
</div> `
|
|
|
|
)}
|
2025-06-19 11:39:16 +00:00
|
|
|
</div>
|
2024-01-10 05:11:55 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2025-06-19 11:39:16 +00:00
|
|
|
public async connectedCallback() {
|
|
|
|
await super.connectedCallback();
|
|
|
|
// Initialize boolean options early
|
|
|
|
if (this.type === 'boolean' && this.options.length === 0) {
|
|
|
|
this.options = [this.booleanTrueName || 'true', this.booleanFalseName || 'false'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async firstUpdated(_changedProperties: Map<string | number | symbol, unknown>) {
|
|
|
|
super.firstUpdated(_changedProperties);
|
|
|
|
// Update boolean options if they changed
|
2024-01-10 05:11:55 +01:00
|
|
|
if (this.type === 'boolean') {
|
2024-01-15 12:57:49 +01:00
|
|
|
this.options = [this.booleanTrueName || 'true', this.booleanFalseName || 'false'];
|
2024-01-10 05:11:55 +01:00
|
|
|
}
|
2025-06-19 11:39:16 +00:00
|
|
|
// Wait for the next frame to ensure DOM is fully rendered
|
|
|
|
await this.updateComplete;
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.setIndicator();
|
|
|
|
});
|
2024-01-10 05:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async handleSelection(optionArg: string) {
|
2025-06-27 21:16:52 +00:00
|
|
|
if (this.disabled) return;
|
2024-01-10 05:11:55 +01:00
|
|
|
this.selectedOption = optionArg;
|
2025-06-27 21:16:52 +00:00
|
|
|
this.requestUpdate();
|
|
|
|
this.changeSubject.next(this);
|
|
|
|
await this.updateComplete;
|
2024-01-10 05:11:55 +01:00
|
|
|
this.setIndicator();
|
|
|
|
}
|
2025-06-27 21:16:52 +00:00
|
|
|
|
2025-06-19 11:39:16 +00:00
|
|
|
private indicatorInitialized = false;
|
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
public async setIndicator() {
|
|
|
|
const indicator: HTMLDivElement = this.shadowRoot.querySelector('.indicator');
|
2025-06-19 11:39:16 +00:00
|
|
|
const selectedIndex = this.options.indexOf(this.selectedOption);
|
|
|
|
|
|
|
|
// If no valid selection, hide indicator
|
|
|
|
if (selectedIndex === -1 || !indicator) {
|
|
|
|
if (indicator) {
|
|
|
|
indicator.style.opacity = '0';
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-18 02:08:19 +01:00
|
|
|
const option: HTMLDivElement = this.shadowRoot.querySelector(
|
2025-06-19 11:39:16 +00:00
|
|
|
`.option:nth-child(${selectedIndex + 2})`
|
2024-01-18 02:08:19 +01:00
|
|
|
);
|
2025-06-19 11:39:16 +00:00
|
|
|
|
2024-01-10 05:11:55 +01:00
|
|
|
if (indicator && option) {
|
2025-06-19 11:39:16 +00:00
|
|
|
// Only disable transition for the very first positioning
|
|
|
|
if (!this.indicatorInitialized) {
|
|
|
|
indicator.classList.add('no-transition');
|
|
|
|
this.indicatorInitialized = true;
|
|
|
|
|
|
|
|
// Remove the no-transition class after a brief delay
|
|
|
|
setTimeout(() => {
|
|
|
|
indicator.classList.remove('no-transition');
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
|
2025-06-27 21:16:52 +00:00
|
|
|
indicator.style.width = `${option.clientWidth}px`;
|
|
|
|
indicator.style.left = `${option.offsetLeft}px`;
|
2024-01-10 05:11:55 +01:00
|
|
|
indicator.style.opacity = '1';
|
|
|
|
}
|
2025-06-19 11:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public getValue(): string | boolean {
|
|
|
|
if (this.type === 'boolean') {
|
|
|
|
return this.selectedOption === this.booleanTrueName;
|
|
|
|
}
|
|
|
|
return this.selectedOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
public setValue(value: string | boolean): void {
|
|
|
|
if (this.type === 'boolean' && typeof value === 'boolean') {
|
|
|
|
this.selectedOption = value ? (this.booleanTrueName || 'true') : (this.booleanFalseName || 'false');
|
|
|
|
} else {
|
|
|
|
this.selectedOption = value as string;
|
|
|
|
}
|
2025-06-27 21:16:52 +00:00
|
|
|
this.requestUpdate();
|
2025-06-19 11:39:16 +00:00
|
|
|
if (this.hasUpdated) {
|
2025-06-27 21:16:52 +00:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.setIndicator();
|
|
|
|
});
|
2025-06-19 11:39:16 +00:00
|
|
|
}
|
2024-01-10 05:11:55 +01:00
|
|
|
}
|
|
|
|
}
|