Skip to content

Commit

Permalink
feat: add discord-ordered-list
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 18, 2024
1 parent 0c9cd70 commit 42f8b49
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/core/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,6 @@ <h1 class="logo">@skyra/discord-components-core</h1>
</div>

<main id="demo">
<h3 class="title">List items</h3>
<discord-messages>
<discord-message>
<discord-unordered-list>
<discord-list-item>This is the top item</discord-list-item>
<discord-unordered-list>
<discord-list-item>This is the first nested item</discord-list-item>
<discord-list-item>This is the second nested item</discord-list-item>
</discord-unordered-list>
</discord-unordered-list>
</discord-message>
</discord-messages>

<h3 class="title">A normal conversation</h3>
<discord-messages>
<discord-message author="Alyx Vargas"> Hey guys, I'm new here! Glad to be able to join you all! </discord-message>
Expand Down Expand Up @@ -602,9 +589,23 @@ <h3 class="title">Headers</h3>
</discord-message>
</discord-messages>

<h3 class="title">Ordered list items</h3>
<discord-messages>
<discord-message profile="favna">
<discord-ordered-list>
<discord-list-item>This is an ordered list item</discord-list-item>
<discord-ordered-list start="99">
<discord-list-item>You can start your list item at a custom number</discord-list-item>
<discord-list-item>We will calculate the length internally</discord-list-item>
<discord-list-item>This whole level is considered to have a start number length of 3</discord-list-item>
</discord-ordered-list>
</discord-ordered-list>
</discord-message>
</discord-messages>

<h3 class="title">Unordered list items</h3>
<discord-messages>
<discord-message>
<discord-message profile="favna">
<discord-unordered-list>
<discord-list-item>Discord</discord-list-item>
<discord-unordered-list>
Expand Down
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"./discord-mention.js": "./dist/components/discord-mention/DiscordMention.js",
"./discord-message.js": "./dist/components/discord-message/DiscordMessage.js",
"./discord-messages.js": "./dist/components/discord-messages/DiscordMessages.js",
"./discord-ordered-list.js": "./dist/components/discord-ordered-list/DiscordOrderedList.js",
"./discord-pre.js": "./dist/components/discord-pre/DiscordPre.js",
"./discord-quote.js": "./dist/components/discord-quote/DiscordQuote.js",
"./discord-reaction.js": "./dist/components/discord-reaction/DiscordReaction.js",
Expand Down Expand Up @@ -71,6 +72,7 @@
"./dist/components/discord-mention/DiscordMention.js",
"./dist/components/discord-message/DiscordMessage.js",
"./dist/components/discord-messages/DiscordMessages.js",
"./dist/components/discord-ordered-list/DiscordOrderedList.js",
"./dist/components/discord-pre/DiscordPre.js",
"./dist/components/discord-quote/DiscordQuote.js",
"./dist/components/discord-reaction/DiscordReaction.js",
Expand Down
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;
}
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export { DiscordListItem } from './components/discord-list-item/DiscordListItem.
export { DiscordMention } from './components/discord-mention/DiscordMention.js';
export { DiscordMessage } from './components/discord-message/DiscordMessage.js';
export { DiscordMessages } from './components/discord-messages/DiscordMessages.js';
export { DiscordOrderedList } from './components/discord-ordered-list/DiscordOrderedList.js';
export { DiscordPre } from './components/discord-pre/DiscordPre.js';
export { DiscordQuote } from './components/discord-quote/DiscordQuote.js';
export { DiscordReaction } from './components/discord-reaction/DiscordReaction.js';
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const DiscordListItem = createReactComponent('discord-list-item', ReactCo
export const DiscordMention = createReactComponent('discord-mention', ReactComponents.DiscordMention);
export const DiscordMessage = createReactComponent('discord-message', ReactComponents.DiscordMessage);
export const DiscordMessages = createReactComponent('discord-messages', ReactComponents.DiscordMessages);
export const DiscordOrderedList = createReactComponent('discord-ordered-list', ReactComponents.DiscordOrderedList);
export const DiscordPre = createReactComponent('discord-pre', ReactComponents.DiscordPre);
export const DiscordQuote = createReactComponent('discord-quote', ReactComponents.DiscordQuote);
export const DiscordReaction = createReactComponent('discord-reaction', ReactComponents.DiscordReaction);
Expand Down

0 comments on commit 42f8b49

Please sign in to comment.