-
-
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.
feat: add discord-unordered-list and discord-list-item
- Loading branch information
Showing
6 changed files
with
184 additions
and
2 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
40 changes: 40 additions & 0 deletions
40
packages/core/src/components/discord-list-item/DiscordListItem.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,40 @@ | ||
import { css, html, LitElement } from 'lit'; | ||
import { customElement } from 'lit/decorators.js'; | ||
import { DiscordComponentsError } from '../../util.js'; | ||
|
||
@customElement('discord-list-item') | ||
export class DiscordListItem extends LitElement { | ||
public static override styles = css` | ||
:host > li { | ||
white-space: break-spaces; | ||
margin-bottom: 4px; | ||
} | ||
`; | ||
|
||
public checkParentElement() { | ||
if ( | ||
this.parentElement?.tagName.toLowerCase() !== 'discord-unordered-list' && | ||
this.parentElement?.tagName.toLowerCase() !== 'discord-ordered-list' | ||
) { | ||
throw new DiscordComponentsError( | ||
'All <discord-list-item> components must be direct children of <discord-unordered-list> or <discord-ordered-list>.' | ||
); | ||
} | ||
} | ||
|
||
protected override render() { | ||
this.checkParentElement(); | ||
|
||
return html`<!-- display: inline --> | ||
<li | ||
><span | ||
><span><slot></slot></span></span | ||
></li>`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'discord-list-item': DiscordListItem; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/core/src/components/discord-unordered-list/DiscordUnorderedList.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,76 @@ | ||
import { css, html, LitElement } from 'lit'; | ||
import { customElement, property } from 'lit/decorators.js'; | ||
import { DiscordComponentsError } from '../../util.js'; | ||
|
||
@customElement('discord-unordered-list') | ||
export class DiscordUnorderedList extends LitElement { | ||
public static override styles = css` | ||
:host > ul { | ||
list-style: disc; | ||
list-style-position: outside; | ||
margin: 4px 0 0 16px; | ||
padding: 0px; | ||
} | ||
:host([nested]) > ul { | ||
list-style: circle; | ||
} | ||
`; | ||
|
||
/** | ||
* Whether this is a nested list or not, this will change the style of the list-style. | ||
* | ||
* The library will try to guess this automatically based on the component tree, but | ||
* you can also set this manually. | ||
* | ||
* @default false | ||
*/ | ||
@property({ type: Boolean, reflect: true }) | ||
public accessor nested = false; | ||
|
||
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-unordered-list> components must be one of <discord-unordered-list>, <discord-ordered-list>, or <discord-list-item>.' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Sets {@link DiscordUnorderedList.nested nested} to true if the parent is either | ||
* `<discord-unordered-list>` or `<discord-ordered-list>`. | ||
*/ | ||
protected override willUpdate(): void { | ||
if ( | ||
this.parentElement?.tagName.toLowerCase() === 'discord-unordered-list' || | ||
this.parentElement?.tagName.toLowerCase() === 'discord-ordered-list' | ||
) { | ||
this.nested = true; | ||
} | ||
} | ||
|
||
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`<ul> | ||
<slot></slot> | ||
</ul>`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'discord-unordered-list': DiscordUnorderedList; | ||
} | ||
} |
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