-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
212 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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,21 @@ | ||
# `<dy-segmented>` | ||
|
||
## Example | ||
|
||
<gbp-example name="dy-segmented" direction="column" src="https://jspm.dev/duoyun-ui/elements/segmented"> | ||
|
||
```json | ||
[ | ||
{ | ||
"value": 0, | ||
"options": [{ "label": "Item" }, { "label": "Item 2" }, { "label": "Item 3" }], | ||
"@change": "(evt) => evt.target.value = evt.detail" | ||
} | ||
] | ||
``` | ||
|
||
</gbp-example> | ||
|
||
## API | ||
|
||
<gbp-api src="/src/elements/segmented.ts"></gbp-api> |
This file contains 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 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,21 @@ | ||
# `<dy-segmented>` | ||
|
||
## Example | ||
|
||
<gbp-example name="dy-segmented" direction="column" src="https://jspm.dev/duoyun-ui/elements/segmented"> | ||
|
||
```json | ||
[ | ||
{ | ||
"value": 0, | ||
"options": [{ "label": "Item" }, { "label": "Item 2" }, { "label": "Item 3" }], | ||
"@change": "(evt) => evt.target.value = evt.detail" | ||
} | ||
] | ||
``` | ||
|
||
</gbp-example> | ||
|
||
## API | ||
|
||
<gbp-api src="/src/elements/segmented.ts"></gbp-api> |
This file contains 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 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 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,122 @@ | ||
import { GemElement, html } from '@mantou/gem/lib/element'; | ||
import { | ||
Emitter, | ||
adoptedStyle, | ||
boolattribute, | ||
customElement, | ||
emitter, | ||
part, | ||
property, | ||
} from '@mantou/gem/lib/decorators'; | ||
import { classMap, createCSSSheet, css, partMap } from '@mantou/gem/lib/utils'; | ||
|
||
import { commonHandle } from '../lib/hotkeys'; | ||
import { theme } from '../lib/theme'; | ||
|
||
import type { Option } from './radio'; | ||
|
||
import './use'; | ||
|
||
const style = createCSSSheet(css` | ||
:host(:where(:not([hidden]))) { | ||
display: flex; | ||
cursor: default; | ||
user-select: none; | ||
font-size: 0.875em; | ||
line-height: 1.2; | ||
border-radius: ${theme.normalRound}; | ||
background: ${theme.hoverBackgroundColor}; | ||
padding: 2px; | ||
} | ||
:host([small]) { | ||
font-size: 0.75em; | ||
} | ||
:host([disabled]) { | ||
cursor: not-allowed; | ||
} | ||
:host([disabled]) .segment * { | ||
opacity: 0.5; | ||
} | ||
.segment { | ||
width: 0; | ||
flex-grow: 1; | ||
display: flex; | ||
justify-content: center; | ||
gap: 0.3em; | ||
padding: calc(0.5em - 1px) 1em; | ||
border-radius: inherit; | ||
min-width: 5em; | ||
} | ||
:host(:where(:not([disabled]))) .segment:hover { | ||
background: ${theme.borderColor}; | ||
} | ||
:host .segment.current { | ||
background: ${theme.backgroundColor}; | ||
} | ||
.icon { | ||
flex-shrink: 0; | ||
width: 1.2em; | ||
} | ||
.label { | ||
white-space: nowrap; | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
} | ||
`); | ||
|
||
export interface SegmentedOption<T = any> extends Option<T> { | ||
icon?: string | Element | DocumentFragment; | ||
} | ||
|
||
/** | ||
* @customElement dy-segmented | ||
*/ | ||
@customElement('dy-segmented') | ||
@adoptedStyle(style) | ||
export class DuoyunSegmentedElement extends GemElement { | ||
@part static segment: string; | ||
@part static currentSegment: string; | ||
@part static icon: string; | ||
|
||
@boolattribute disabled: boolean; | ||
@boolattribute small: boolean; | ||
|
||
@property options?: SegmentedOption[]; | ||
@property value?: any; | ||
@emitter change: Emitter<any>; | ||
|
||
constructor() { | ||
super({ delegatesFocus: true }); | ||
this.internals.role = 'group'; | ||
} | ||
|
||
#onClick = (v: any) => { | ||
if (this.disabled) return; | ||
this.change(v); | ||
}; | ||
|
||
render = () => { | ||
if (!this.options) return html``; | ||
return html` | ||
${this.options.map(({ value, label, icon }, index) => { | ||
const isCurrent = (value ?? index) === this.value; | ||
return html` | ||
<div | ||
role="radio" | ||
tabindex="0" | ||
class=${classMap({ segment: true, current: isCurrent })} | ||
part=${partMap({ | ||
[DuoyunSegmentedElement.segment]: true, | ||
[DuoyunSegmentedElement.currentSegment]: isCurrent, | ||
})} | ||
@keydown=${commonHandle} | ||
@click=${() => this.#onClick(value ?? index)} | ||
> | ||
${icon ? html`<dy-use part=${DuoyunSegmentedElement.icon} class="icon" .element=${icon}></dy-use>` : ''} | ||
<span class="label">${label}</span> | ||
</div> | ||
`; | ||
})} | ||
`; | ||
}; | ||
} |
This file contains 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 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