-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
5 changed files
with
98 additions
and
14 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
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
79 changes: 79 additions & 0 deletions
79
packages/core/src/components/discord-ordered-list/DiscordOrderedList.ts
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,79 @@ | ||
import { css, html, LitElement } from 'lit'; | ||
import { customElement, property, state } from 'lit/decorators.js'; | ||
import { DiscordComponentsError } from '../../util.js'; | ||
|
||
@customElement('discord-ordered-list') | ||
export class DiscordOrderedList extends LitElement { | ||
public static override styles = css` | ||
:host > ol { | ||
list-style-image: initial; | ||
list-style-type: decimal; | ||
list-style-position: outside; | ||
margin-bottom: 0px; | ||
margin-top: 4px; | ||
margin-right: 0px; | ||
margin-left: calc(0.4em + 0.6em * var(--totalCharacters)); | ||
padding: 0px; | ||
} | ||
`; | ||
|
||
/** | ||
* The starting number for the ordered list. | ||
* | ||
* You can set this to start the list at a specific number | ||
* | ||
* @default 1 | ||
*/ | ||
@property({ type: Number, reflect: true }) | ||
public accessor start = 1; | ||
|
||
@state() | ||
private accessor startLength = 1; | ||
|
||
public checkChildren() { | ||
const allChildrenAreListItems = Array.from(this.children).every((child) => { | ||
const tagNameLowerCase = child.tagName.toLowerCase(); | ||
return ( | ||
tagNameLowerCase === 'discord-list-item' || | ||
tagNameLowerCase === 'discord-unordered-list' || | ||
tagNameLowerCase === 'discord-ordered-list' | ||
); | ||
}); | ||
|
||
if (!allChildrenAreListItems) { | ||
throw new DiscordComponentsError( | ||
'All direct children inside of a <discord-ordered-list> components must be one of <discord-unordered-list>, <discord-ordered-list>, or <discord-list-item>.' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Checks how many children of `<discord-list-item>` this component has and updates | ||
* the {@link DiscordOrderedList.startLength startLength} state. | ||
*/ | ||
protected override willUpdate(): void { | ||
const amountOfListItems = Array.from(this.children).filter((child) => { | ||
return child.tagName.toLowerCase() === 'discord-list-item'; | ||
}).length; | ||
|
||
const totalCount = this.start + amountOfListItems; | ||
|
||
this.startLength = totalCount.toString().length; | ||
} | ||
|
||
protected override render() { | ||
this.checkChildren(); | ||
|
||
// We disable the eslint rule here because users should use the <discord-list-item> component inside of this component. | ||
// eslint-disable-next-line lit-a11y/list | ||
return html`<ol start=${this.start} style="--totalCharacters:${this.startLength}"> | ||
<slot></slot> | ||
</ol>`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'discord-ordered-list': DiscordOrderedList; | ||
} | ||
} |
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