Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions demo/story-components/story-prop-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
defaultValue: string | boolean | number;
inputType?: 'text' | 'radio' | 'number';
radioOptions?: string[] | boolean[];
/* Groups consecutive inputs under a shared heading */
section?: string;
};

export type PropInputData = {
settings: PropInputSettings<any>[];

Check warning on line 27 in demo/story-components/story-prop-settings.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
};

export type AppliedProps = {
Expand All @@ -46,14 +48,23 @@
return html`
<div class="settings-options">
<table>
${this.propInputData.settings.map(
(input) =>
choose(
${this.propInputData.settings.map((input, index) => {
const previous = this.propInputData?.settings[index - 1];
const startsSection =
!!input.section && input.section !== previous?.section;
return html`
${startsSection
? html`<tr>
<th class="prop-section" colspan="2">${input.section}</th>
</tr>`
: nothing}
${choose(
input.inputType,
[['radio', () => this.createRadioPropInput(input)]],
() => this.createDefaultPropInput(input),
) ?? nothing,
)}
) ?? nothing}
`;
})}
</table>
<button @click=${this.applyProps}>Apply</button>
</div>
Expand All @@ -61,7 +72,7 @@
}

private createDefaultPropInput(
settings: PropInputSettings<any>,

Check warning on line 75 in demo/story-components/story-prop-settings.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
): TemplateResult | typeof nothing {
const inputId = labelToId(settings.label);

Expand All @@ -83,7 +94,7 @@
}

private createRadioPropInput(
settings: PropInputSettings<any>,

Check warning on line 97 in demo/story-components/story-prop-settings.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
): TemplateResult | typeof nothing {
if (settings.inputType !== 'radio' || !settings.radioOptions)
return nothing;
Expand All @@ -105,6 +116,7 @@
data-prop=${settings.propertyName}
data-format=${typeof settings.defaultValue}
?checked=${settings.defaultValue === option}
@change=${this.applyProps}
/><label for="${inputId}-${option}"> ${option} </label>`,
)}
</td>
Expand Down Expand Up @@ -138,10 +150,19 @@
break;
}

// Always apply, so switching back to a default really resets the demo.
appliedProps.push({ propName, value });

// But leave defaults out of the example — consumers only need to pass
// what they are actually changing.
const setting = this.propInputData?.settings.find(
(candidate) => candidate.propertyName === propName,
);
if (setting && value === setting.defaultValue) return;

const stringifiedValue =
typeof value === 'string' ? `'${value}'` : value.toString();
stringifiedProps.push(`.${propName}=\${${stringifiedValue}}`);
appliedProps.push({ propName, value });
});

this.dispatchEvent(
Expand All @@ -162,6 +183,12 @@
background-color: var(--primary-background-color);
padding: 1em;
}

.prop-section {
text-align: left;
padding-top: 0.75em;
font-size: var(--font-size-standard--, 1em);
}
`,
];
}
Expand Down
186 changes: 156 additions & 30 deletions demo/story-components/story-styles-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import { labelToId } from '../story-utils';

export type StyleInputType = 'color' | 'text' | 'number' | 'range';

/**
* A one-click value for a style input, for surfacing the values real consumers
* use rather than making someone type them.
*/
export type StyleInputPreset = {
label: string;
value: string | number;
/* Optional hint, shown as the button's tooltip */
note?: string;
};

export type StyleInputSettings = {
label: string;
cssVariable: string;
Expand All @@ -25,6 +36,11 @@ export type StyleInputSettings = {
max?: number;
step?: number;
unit?: string;
presets?: StyleInputPreset[];
/* Sits the presets beside the input instead of beneath it */
presetsInline?: boolean;
/* Groups consecutive inputs under a shared heading */
section?: string;
};

/**
Expand Down Expand Up @@ -81,9 +97,21 @@ export class StoryStylesSettings extends LitElement {
into <table> get hoisted into an implicit tbody by the parser,
which ejects Lit's marker nodes and breaks later re-renders. -->
<tbody>
${this.styleInputData.settings.map((input) =>
this.renderStyleRow(input),
)}
${this.styleInputData.settings.map((input, index) => {
const previous = this.styleInputData?.settings[index - 1];
const startsSection =
!!input.section && input.section !== previous?.section;
return html`
${startsSection
? html`<tr>
<th class="style-section" colspan="2">
${input.section}
</th>
</tr>`
: nothing}
${this.renderStyleRow(input)}
`;
})}
</tbody>
</table>
<button @click=${this.applyStyles}>Apply</button>
Expand Down Expand Up @@ -162,7 +190,11 @@ export class StoryStylesSettings extends LitElement {
});

this.dispatchEvent(
new CustomEvent('stylesApplied', { detail: { styles: '' } }),
new CustomEvent('stylesApplied', {
detail: { styles: '' },
bubbles: true,
composed: true,
}),
);
}

Expand All @@ -178,33 +210,42 @@ export class StoryStylesSettings extends LitElement {
<td>
<label for=${inputId}>${input.label}</label>
</td>
<td class="style-input-cell">
<input
id=${inputId}
class="style-input"
type=${input.inputType ?? 'text'}
min=${ifDefined(isNumeric ? input.min : undefined)}
max=${ifDefined(isNumeric ? input.max : undefined)}
step=${ifDefined(isNumeric ? input.step : undefined)}
value=${input.defaultValue}
data-variable=${input.cssVariable}
data-unit=${ifDefined(input.unit)}
@input=${input.inputType === 'range'
? this.updateRangeReadout
: undefined}
/>
${input.inputType === 'range'
? html`<output class="style-readout" for=${inputId}
>${this.readoutFor(input)}</output
>`
<td
class="style-input-cell ${input.presetsInline
? 'presets-inline'
: ''}"
>
<div class="style-input-row">
<input
id=${inputId}
class="style-input"
type=${input.inputType ?? 'text'}
min=${ifDefined(isNumeric ? input.min : undefined)}
max=${ifDefined(isNumeric ? input.max : undefined)}
step=${ifDefined(isNumeric ? input.step : undefined)}
value=${input.defaultValue}
data-variable=${input.cssVariable}
data-unit=${ifDefined(input.unit)}
@input=${input.inputType === 'range'
? this.updateRangeReadout
: undefined}
/>
${input.inputType === 'range'
? html`<output class="style-readout" for=${inputId}
>${this.readoutFor(input)}</output
>`
: nothing}
${when(
this.styleInputData?.showCssVariables,
() =>
html`<code class="style-var" title=${input.cssVariable}
>${input.cssVariable}</code
>`,
)}
</div>
${input.presets
? this.renderPresets(inputId, input.presets)
: nothing}
${when(
this.styleInputData?.showCssVariables,
() =>
html`<code class="style-var" title=${input.cssVariable}
>${input.cssVariable}</code
>`,
)}
</td>
</tr>
`;
Expand All @@ -230,6 +271,46 @@ export class StoryStylesSettings extends LitElement {
}

/** The text to show beside a range slider — live value, else its default. */
/**
* Renders the preset buttons for a style input. Choosing one fills the input
* and applies immediately, so it takes one click rather than two.
*/
private renderPresets(
inputId: string,
presets: StyleInputPreset[],
): TemplateResult {
return html`
<div class="style-presets">
${presets.map(
(preset) => html`
<button
type="button"
class="style-preset"
title=${ifDefined(preset.note)}
@click=${() => this.applyPreset(inputId, preset)}
>
${preset.label}
<small>${preset.value}</small>
</button>
`,
)}
</div>
`;
}

private applyPreset(inputId: string, preset: StyleInputPreset): void {
const input = this.renderRoot.querySelector<HTMLInputElement>(
`#${CSS.escape(inputId)}`,
);
if (!input) return;

input.value = `${preset.value}`;
if (input.type === 'range') {
input.dispatchEvent(new Event('input'));
}
this.applyStyles();
}

private readoutFor(input: StyleInputSettings): string {
return (
this.rangeReadouts[input.cssVariable] ??
Expand All @@ -252,6 +333,9 @@ export class StoryStylesSettings extends LitElement {
this.dispatchEvent(
new CustomEvent('stylesApplied', {
detail: { styles: appliedStyles.join('\n ') },
// Crosses the shadow boundary so a story can react to its own vars
bubbles: true,
composed: true,
}),
);
}
Expand All @@ -267,7 +351,49 @@ export class StoryStylesSettings extends LitElement {

.style-input-cell {
display: flex;
flex-direction: column;
align-items: flex-start;
}

.style-input-cell.presets-inline {
flex-direction: row;
align-items: center;
gap: 0.5rem;
}

.style-input-cell.presets-inline .style-presets {
margin-top: 0;
}

.style-section {
text-align: left;
padding-top: 0.75em;
font-size: var(--font-size-standard--, 1em);
}

.style-input-row {
display: flex;
align-items: center;
}

.style-presets {
display: flex;
flex-wrap: wrap;
gap: 0.3rem;
margin-top: 0.35rem;
}

.style-preset {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.1rem;
padding: 0.25rem 0.4rem;
cursor: pointer;
}

.style-preset small {
opacity: 0.7;
}

.style-readout {
Expand Down
4 changes: 3 additions & 1 deletion demo/story-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,9 @@ export class StoryTemplate extends LitElement {
private handlePropsApplied(e: CustomEvent): void {
const stringifiedProps = e.detail.stringifiedProps;
const appliedProps: AppliedProps = e.detail.appliedProps;
if (!stringifiedProps || !appliedProps) return;
// An empty string is meaningful: every prop is at its default, so the
// example should show none of them.
if (typeof stringifiedProps !== 'string' || !appliedProps) return;

this.stringifiedProps = stringifiedProps;
appliedProps.forEach((prop) => {
Expand Down
2 changes: 1 addition & 1 deletion src/elements/ia-item-navigator/ia-item-navigator-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import './menus/ia-itemnav-sort-files-button';
import './menus/ia-itemnav-share-panel';
import { viewableFilesIcon } from './menus/ia-itemnav-viewable-files-panel';
import { shareIcon } from './menus/ia-itemnav-share-panel';
import { maskedIcon } from './masked-icon';
import { maskedIcon } from '@src/util/masked-icon';
import listIconUrl from './menus/icons/list.svg';
import type {
MenuProviderInterface,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, expect, test, vi } from 'vitest';

import { IAItemNavMenuButton } from './ia-itemnav-menu-button';
import './ia-itemnav-menu-button';
import { maskedIcon } from './masked-icon';
import { maskedIcon } from '@src/util/masked-icon';

describe('IAItemNavMenuButton', () => {
test('renders a <button> by default', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/elements/ia-item-navigator/icons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maskedIcon } from './masked-icon';
import { maskedIcon } from '@src/util/masked-icon';

import ellipsesUrl from './icons/ellipses.svg';
import collapseSidebarUrl from './icons/collapse-sidebar.svg';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'lit';
import { customElement, property } from 'lit/decorators.js';
import themeStyles from '@src/themes/theme-styles';
import { maskedIcon } from '../masked-icon';
import { maskedIcon } from '@src/util/masked-icon';
import sortAscUrl from './icons/sort-asc.svg';
import sortDescUrl from './icons/sort-desc.svg';
import sortNeutralUrl from './icons/sort-neutral.svg';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { customElement, property } from 'lit/decorators.js';
import { repeat } from 'lit/directives/repeat.js';
import themeStyles from '@src/themes/theme-styles';
import { maskedIcon } from '../masked-icon';
import { maskedIcon } from '@src/util/masked-icon';
import viewableFilesUrl from './icons/viewable-files.svg';
import type { FileSortOption, ViewableFileInfo } from './models';

Expand Down
2 changes: 1 addition & 1 deletion src/elements/ia-item-navigator/menus/share-icons.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type TemplateResult } from 'lit';
import { maskedIcon } from '../masked-icon';
import { maskedIcon } from '@src/util/masked-icon';

import shareUrl from './icons/share.svg';
import twitterUrl from './icons/twitter.svg';
Expand Down
Loading
Loading