-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add <ep-select> form select (Etherpad nice-select look) #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JohnMcLear
wants to merge
1
commit into
main
Choose a base branch
from
feat/ep-select
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| import { LitElement, html, css } from 'lit'; | ||
| import { customElement, property, state } from 'lit/decorators.js'; | ||
| import { classMap } from 'lit/directives/class-map.js'; | ||
|
|
||
| export interface EpSelectOption { | ||
| value: string; | ||
| label: string; | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * `<ep-select>` — a styled form select, matching Etherpad colibris's | ||
| * nice-select look (the plugin Etherpad uses to skin native `<select>`s). | ||
| * | ||
| * @fires ep-select:change - When a value is chosen. Detail: `{ value, label }` | ||
| */ | ||
| @customElement('ep-select') | ||
| export class EpSelect extends LitElement { | ||
| static styles = css` | ||
| :host { | ||
| --ep-font: var(--main-font-family, Quicksand, Cantarell, "Open Sans", "Helvetica Neue", sans-serif); | ||
| display: inline-block; | ||
| font-family: var(--ep-font); | ||
| font-size: 14px; | ||
| } | ||
|
|
||
| /* The box — base nice-select geometry + colibris colours. */ | ||
| .nice-select { | ||
| position: relative; | ||
| box-sizing: border-box; | ||
| display: inline-block; | ||
| width: 100%; | ||
| min-width: 100px; | ||
| height: 28px; | ||
| line-height: 28px; | ||
| padding: 0 25px 0 10px; | ||
| border-radius: 3px; | ||
| border: 1px solid var(--bg-soft-color, #f2f3f4); | ||
| background-color: var(--bg-soft-color, #f2f3f4); | ||
| color: var(--text-color, #485365); | ||
| font-weight: bold; | ||
| cursor: pointer; | ||
| outline: none; | ||
| white-space: nowrap; | ||
| user-select: none; | ||
| transition: border-color 0.1s ease-in-out; | ||
| } | ||
| .nice-select:hover { border-color: var(--middle-color, #d2d2d2); } | ||
| .nice-select:focus-visible { border-color: var(--dark-color, #576273); } | ||
|
|
||
| .current { display: block; overflow: hidden; text-overflow: ellipsis; } | ||
| :host(:not([value])) .current, | ||
| .current.placeholder { color: var(--text-soft-color, #576273); font-weight: normal; } | ||
|
|
||
| /* Chevron */ | ||
| .nice-select::after { | ||
| content: ''; | ||
| position: absolute; | ||
| top: 50%; | ||
| right: 12px; | ||
| width: 5px; | ||
| height: 5px; | ||
| margin-top: -4px; | ||
| border-bottom: 2px solid var(--text-soft-color, #999); | ||
| border-right: 2px solid var(--text-soft-color, #999); | ||
| transform-origin: 66% 66%; | ||
| transform: rotate(45deg); | ||
| transition: transform 0.15s ease-in-out; | ||
| pointer-events: none; | ||
| } | ||
| :host([open]) .nice-select::after { transform: rotate(-135deg); } | ||
|
|
||
| /* Dropdown list */ | ||
| .list { | ||
| list-style: none; | ||
| margin: 4px 0 0; | ||
| padding: 4px 0; | ||
| position: absolute; | ||
| top: 100%; | ||
| left: 0; | ||
| min-width: 100%; | ||
| box-sizing: border-box; | ||
| background-color: var(--bg-soft-color, #f2f3f4); | ||
| border-radius: 3px; | ||
| box-shadow: 0 0 0 1px rgba(68, 68, 68, 0.11), 0 8px 16px rgba(27, 39, 51, 0.12); | ||
| opacity: 0; | ||
| pointer-events: none; | ||
| max-height: 0; | ||
| overflow: auto; | ||
| z-index: 9; | ||
| transform-origin: 50% 0; | ||
| transform: scale(0.75) translateY(-12px); | ||
| transition: transform 0.2s cubic-bezier(0.5, 0, 0.08, 1.1), opacity 0.15s ease-out; | ||
| } | ||
| :host([open]) .list { | ||
| opacity: 1; | ||
| pointer-events: auto; | ||
| max-height: 260px; | ||
| transform: scale(1) translateY(0); | ||
| } | ||
|
|
||
| .option { | ||
| padding: 0 15px; | ||
| min-height: 35px; | ||
| line-height: 35px; | ||
| cursor: pointer; | ||
| white-space: nowrap; | ||
| font-weight: normal; | ||
| color: var(--text-color, #485365); | ||
| transition: background-color 0.2s; | ||
| } | ||
| .option:hover, | ||
| .option.focus, | ||
| .option.selected.focus { background-color: var(--bg-color, #ffffff); } | ||
| .option.selected { font-weight: bold; } | ||
| .option.disabled { | ||
| color: var(--text-soft-color, #999); | ||
| background-color: transparent; | ||
| cursor: default; | ||
| } | ||
|
|
||
| /* Disabled box */ | ||
| :host([disabled]) .nice-select { | ||
| color: var(--text-soft-color, #999); | ||
| pointer-events: none; | ||
| opacity: 0.7; | ||
| } | ||
| :host([disabled]) .nice-select::after { border-color: var(--middle-color, #ccc); } | ||
| `; | ||
|
|
||
| @property({ type: Array }) options: EpSelectOption[] = []; | ||
| @property({ reflect: true }) value = ''; | ||
| @property() placeholder = ''; | ||
| @property() name = ''; | ||
| @property({ type: Boolean, reflect: true }) disabled = false; | ||
| @property({ type: Boolean, reflect: true }) open = false; | ||
|
|
||
| @state() private _focusIndex = -1; | ||
|
|
||
| private _onDocClick = (e: MouseEvent) => { | ||
| if (this.open && !e.composedPath().includes(this)) this.open = false; | ||
| }; | ||
|
|
||
| connectedCallback() { | ||
| super.connectedCallback(); | ||
| document.addEventListener('click', this._onDocClick); | ||
| } | ||
|
|
||
| disconnectedCallback() { | ||
| super.disconnectedCallback(); | ||
| document.removeEventListener('click', this._onDocClick); | ||
| } | ||
|
|
||
| private get _selected(): EpSelectOption | undefined { | ||
| return this.options.find((o) => o.value === this.value); | ||
| } | ||
|
|
||
| private _toggle() { | ||
| if (this.disabled) return; | ||
| this.open = !this.open; | ||
| if (this.open) { | ||
| const sel = this.options.findIndex((o) => o.value === this.value); | ||
| this._focusIndex = sel >= 0 ? sel : this._firstEnabled(); | ||
| } | ||
| } | ||
|
|
||
| private _firstEnabled(): number { | ||
| return this.options.findIndex((o) => !o.disabled); | ||
| } | ||
|
|
||
| private _select(opt: EpSelectOption, e?: Event) { | ||
| e?.stopPropagation(); | ||
| if (opt.disabled) return; | ||
| this.value = opt.value; | ||
| this.open = false; | ||
| this.dispatchEvent(new CustomEvent('ep-select:change', { | ||
| detail: { value: opt.value, label: opt.label }, | ||
| bubbles: true, | ||
| composed: true, | ||
| })); | ||
| } | ||
|
|
||
| private _move(dir: 1 | -1) { | ||
| const n = this.options.length; | ||
| let i = this._focusIndex; | ||
| for (let step = 0; step < n; step++) { | ||
| i = (i + dir + n) % n; | ||
| if (!this.options[i]?.disabled) { this._focusIndex = i; break; } | ||
| } | ||
| } | ||
|
|
||
| private _onKeydown(e: KeyboardEvent) { | ||
| if (this.disabled) return; | ||
| switch (e.key) { | ||
| case 'Enter': | ||
| case ' ': | ||
| e.preventDefault(); | ||
| if (this.open && this._focusIndex >= 0) this._select(this.options[this._focusIndex]); | ||
| else this._toggle(); | ||
| break; | ||
| case 'ArrowDown': | ||
| e.preventDefault(); | ||
| if (!this.open) this._toggle(); | ||
| else this._move(1); | ||
| break; | ||
| case 'ArrowUp': | ||
| e.preventDefault(); | ||
| if (this.open) this._move(-1); | ||
| break; | ||
| case 'Escape': | ||
| this.open = false; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| const sel = this._selected; | ||
| return html` | ||
| <div | ||
| class="nice-select" | ||
| part="select" | ||
| role="listbox" | ||
| aria-expanded="${this.open}" | ||
| tabindex="${this.disabled ? -1 : 0}" | ||
| @click="${this._toggle}" | ||
| @keydown="${this._onKeydown}" | ||
| > | ||
| <span class="current ${classMap({ placeholder: !sel })}">${sel ? sel.label : this.placeholder}</span> | ||
| <ul class="list" part="list" role="presentation"> | ||
| ${this.options.map((o, i) => html` | ||
| <li | ||
| class="option ${classMap({ selected: o.value === this.value, disabled: !!o.disabled, focus: i === this._focusIndex })}" | ||
| role="option" | ||
| aria-selected="${o.value === this.value}" | ||
| data-value="${o.value}" | ||
| @click="${(e: Event) => this._select(o, e)}" | ||
| >${o.label}</li> | ||
| `)} | ||
| </ul> | ||
| </div> | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'ep-select': EpSelect; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { html } from 'lit'; | ||
| import type { Meta, StoryObj } from '@storybook/web-components'; | ||
| import { expect, userEvent, fn, waitFor } from 'storybook/test'; | ||
| import '../src/EpSelect.js'; | ||
|
|
||
| type EpSelectArgs = { | ||
| value: string; | ||
| placeholder: string; | ||
| disabled: boolean; | ||
| }; | ||
|
|
||
| const OPTIONS = [ | ||
| { value: 'h1', label: 'Heading 1' }, | ||
| { value: 'h2', label: 'Heading 2' }, | ||
| { value: 'p', label: 'Paragraph' }, | ||
| { value: 'code', label: 'Code', disabled: true }, | ||
| ]; | ||
|
|
||
| const meta: Meta<EpSelectArgs> = { | ||
| title: 'Components/EpSelect', | ||
| component: 'ep-select', | ||
| args: { value: '', placeholder: 'Select…', disabled: false }, | ||
| }; | ||
| export default meta; | ||
| type Story = StoryObj<EpSelectArgs>; | ||
|
|
||
| async function getSelect(canvasElement: HTMLElement) { | ||
| const host = canvasElement.querySelector('ep-select')! as any; | ||
| host.options = OPTIONS; | ||
| await host.updateComplete; | ||
| const root = host.shadowRoot!; | ||
| return { | ||
| host, | ||
| box: root.querySelector('.nice-select') as HTMLElement, | ||
| current: root.querySelector('.current') as HTMLElement, | ||
| list: root.querySelector('.list') as HTMLElement, | ||
| options: () => Array.from(root.querySelectorAll('.option')) as HTMLElement[], | ||
| }; | ||
| } | ||
|
|
||
| export const Default: Story = { | ||
| render: (a) => html`<ep-select placeholder="${a.placeholder}"></ep-select>`, | ||
| play: async ({ canvasElement }) => { | ||
| const { box, current, options } = await getSelect(canvasElement); | ||
| await expect(box).not.toBe(null); | ||
| // shows the placeholder when nothing is selected | ||
| await expect(current.textContent?.trim()).toBe('Select…'); | ||
| // renders one .option per provided option | ||
| await expect(options().length).toBe(OPTIONS.length); | ||
| }, | ||
| }; | ||
|
|
||
| export const OpensOnClick: Story = { | ||
| render: () => html`<ep-select placeholder="Select…"></ep-select>`, | ||
| play: async ({ canvasElement }) => { | ||
| const { host, box } = await getSelect(canvasElement); | ||
| await expect(host.hasAttribute('open')).toBe(false); | ||
| await userEvent.click(box); | ||
| await waitFor(() => expect(host.hasAttribute('open')).toBe(true)); | ||
| }, | ||
| }; | ||
|
|
||
| export const SelectsOption: Story = { | ||
| render: () => html`<ep-select placeholder="Select…"></ep-select>`, | ||
| play: async ({ canvasElement }) => { | ||
| const { host, box, current, options } = await getSelect(canvasElement); | ||
| const handler = fn(); | ||
| host.addEventListener('ep-select:change', handler); | ||
|
|
||
| await userEvent.click(box); | ||
| await userEvent.click(options()[1]); // Heading 2 | ||
|
|
||
| await expect(host.value).toBe('h2'); | ||
| await expect(current.textContent?.trim()).toBe('Heading 2'); | ||
| await expect(host.hasAttribute('open')).toBe(false); // closes after select | ||
| await expect(handler).toHaveBeenCalledTimes(1); | ||
| await expect(handler.mock.calls[0][0].detail).toEqual({ value: 'h2', label: 'Heading 2' }); | ||
| }, | ||
| }; | ||
|
|
||
| export const PreselectedValue: Story = { | ||
| render: () => html`<ep-select value="p"></ep-select>`, | ||
| play: async ({ canvasElement }) => { | ||
| const { current, options } = await getSelect(canvasElement); | ||
| await expect(current.textContent?.trim()).toBe('Paragraph'); | ||
| const selected = options().find((o) => o.classList.contains('selected')); | ||
| await expect(selected?.textContent?.trim()).toBe('Paragraph'); | ||
| }, | ||
| }; | ||
|
|
||
| export const DisabledDoesNotOpen: Story = { | ||
| render: () => html`<ep-select placeholder="Select…" disabled></ep-select>`, | ||
| play: async ({ canvasElement }) => { | ||
| const { host, box } = await getSelect(canvasElement); | ||
| // pointer-events:none already blocks real clicks; bypass that to confirm | ||
| // the JS guard keeps it closed too. | ||
| await userEvent.click(box, { pointerEventsCheck: 0 }); | ||
| await expect(host.hasAttribute('open')).toBe(false); | ||
| }, | ||
| }; | ||
|
|
||
| export const DisabledOptionNotSelectable: Story = { | ||
| render: () => html`<ep-select placeholder="Select…"></ep-select>`, | ||
| play: async ({ canvasElement }) => { | ||
| const { host, box, options } = await getSelect(canvasElement); | ||
| await userEvent.click(box); | ||
| const codeOption = options().find((o) => o.textContent?.trim() === 'Code')!; | ||
| await expect(codeOption.classList.contains('disabled')).toBe(true); | ||
| await userEvent.click(codeOption); | ||
| await expect(host.value).toBe(''); // disabled option does not select | ||
| }, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Stale focusindex crash
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools