Skip to content

Commit

Permalink
feat: add discord-unordered-list and discord-list-item
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Feb 18, 2024
1 parent b072bea commit 71d483d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 2 deletions.
58 changes: 58 additions & 0 deletions packages/core/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ <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 @@ -588,6 +601,51 @@ <h3 class="title">Headers</h3>
<discord-header level="3">This is a header 3</discord-header>
</discord-message>
</discord-messages>

<h3 class="title">Unordered list items</h3>
<discord-messages>
<discord-message>
<discord-unordered-list>
<discord-list-item>Discord</discord-list-item>
<discord-unordered-list>
<discord-list-item>imposes</discord-list-item>
<discord-unordered-list>
<discord-list-item>a</discord-list-item>
<discord-unordered-list>
<discord-list-item>limit</discord-list-item>
<discord-unordered-list>
<discord-list-item>of</discord-list-item>
<discord-unordered-list>
<discord-list-item>eleven</discord-list-item>
<discord-unordered-list>
<discord-list-item>levels</discord-list-item>
<discord-unordered-list>
<discord-list-item>but</discord-list-item>
<discord-unordered-list>
<discord-list-item>we</discord-list-item>
<discord-unordered-list>
<discord-list-item>can</discord-list-item>
<discord-unordered-list>
<discord-list-item>go (this is 11)</discord-list-item>
<discord-unordered-list>
<discord-list-item>deeper</discord-list-item>
<discord-unordered-list>
<discord-list-item>ain't that cool</discord-list-item>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-unordered-list>
</discord-message>
</discord-messages>
</main>
</div>
</body>
Expand Down
8 changes: 6 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"./discord-invite.js": "./dist/components/discord-invite/DiscordInvite.js",
"./discord-italic.js": "./dist/components/discord-italic/DiscordItalic.js",
"./discord-link.js": "./dist/components/discord-link/DiscordLink.js",
"./discord-list-item.js": "./dist/components/discord-list-item/DiscordListItem.js",
"./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",
Expand All @@ -43,7 +44,8 @@
"./discord-thread.js": "./dist/components/discord-thread/DiscordThread.js",
"./discord-thread-message.js": "./dist/components/discord-thread-message/DiscordThreadMessage.js",
"./discord-time.js": "./dist/components/discord-time/DiscordTime.js",
"./discord-underlined.js": "./dist/components/discord-underlined/DiscordUnderlined.js"
"./discord-underlined.js": "./dist/components/discord-underlined/DiscordUnderlined.js",
"./discord-unordered-list.js": "./dist/components/discord-unordered-list/DiscordUnorderedList.js"
},
"sideEffects": [
"./dist/index.js",
Expand All @@ -65,6 +67,7 @@
"./dist/components/discord-invite/DiscordInvite.js",
"./dist/components/discord-italic/DiscordItalic.js",
"./dist/components/discord-link/DiscordLink.js",
"./dist/components/discord-list-item/DiscordListItem.js",
"./dist/components/discord-mention/DiscordMention.js",
"./dist/components/discord-message/DiscordMessage.js",
"./dist/components/discord-messages/DiscordMessages.js",
Expand All @@ -79,7 +82,8 @@
"./dist/components/discord-thread/DiscordThread.js",
"./dist/components/discord-thread-message/DiscordThreadMessage.js",
"./dist/components/discord-time/DiscordTime.js",
"./dist/components/discord-underlined/DiscordUnderlined.js"
"./dist/components/discord-underlined/DiscordUnderlined.js",
"./dist/components/discord-unordered-list/DiscordUnorderedList.js"
],
"homepage": "https://github.com/skyra-project/discord-components/tree/main/packages/core#readme",
"scripts": {
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/components/discord-list-item/DiscordListItem.ts
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;
}
}
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;
}
}
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { DiscordHeader } from './components/discord-header/DiscordHeader.js';
export { DiscordInvite } from './components/discord-invite/DiscordInvite.js';
export { DiscordItalic } from './components/discord-italic/DiscordItalic.js';
export { DiscordLink } from './components/discord-link/DiscordLink.js';
export { DiscordListItem } from './components/discord-list-item/DiscordListItem.js';
export { DiscordMention } from './components/discord-mention/DiscordMention.js';
export { DiscordMessage } from './components/discord-message/DiscordMessage.js';
export { DiscordMessages } from './components/discord-messages/DiscordMessages.js';
Expand All @@ -35,6 +36,7 @@ export { DiscordThread } from './components/discord-thread/DiscordThread.js';
export { DiscordThreadMessage } from './components/discord-thread-message/DiscordThreadMessage.js';
export { DiscordTime } from './components/discord-time/DiscordTime.js';
export { DiscordUnderlined } from './components/discord-underlined/DiscordUnderlined.js';
export { DiscordUnorderedList } from './components/discord-unordered-list/DiscordUnorderedList.js';

/* EXPORTS END */
export { getConfig, setConfig } from './config.js';
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const DiscordHeader = createReactComponent('discord-header', ReactCompone
export const DiscordInvite = createReactComponent('discord-invite', ReactComponents.DiscordInvite);
export const DiscordItalic = createReactComponent('discord-italic', ReactComponents.DiscordItalic);
export const DiscordLink = createReactComponent('discord-link', ReactComponents.DiscordLink);
export const DiscordListItem = createReactComponent('discord-list-item', ReactComponents.DiscordListItem);
export const DiscordMention = createReactComponent('discord-mention', ReactComponents.DiscordMention);
export const DiscordMessage = createReactComponent('discord-message', ReactComponents.DiscordMessage);
export const DiscordMessages = createReactComponent('discord-messages', ReactComponents.DiscordMessages);
Expand All @@ -36,6 +37,7 @@ export const DiscordThread = createReactComponent('discord-thread', ReactCompone
export const DiscordThreadMessage = createReactComponent('discord-thread-message', ReactComponents.DiscordThreadMessage);
export const DiscordTime = createReactComponent('discord-time', ReactComponents.DiscordTime);
export const DiscordUnderlined = createReactComponent('discord-underlined', ReactComponents.DiscordUnderlined);
export const DiscordUnorderedList = createReactComponent('discord-unordered-list', ReactComponents.DiscordUnorderedList);

/* IMPORTS END */

Expand Down

0 comments on commit 71d483d

Please sign in to comment.