Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspiration demo controls #5157

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions components/alert/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ class Alert extends LocalizeCoreElement(RtlMixin(LitElement)) {
this.type = 'default';
}

static get demoProperties() {
return {
type: { type: 'select', options: ['default', 'critical', 'success', 'warning'], label: 'Alert Type' },
subtext: { type: 'text', label: 'Subtext' },
buttonText: { type: 'text', attribute: 'button-text', label: 'Button Text' },
hasCloseButton: { type: 'switch', attribute: 'has-close-button', label: 'Has Close Button' },
noPadding: { type: 'switch', attribute: 'no-padding', label: 'No Padding' }
};
}

Comment on lines +174 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

I don't want to make it more complex but a future idea might be allowing you to see the different types within the same demo or something. I still think there is merit in certain properties being shown simultaneously for comparison purposes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also am still having internal debates on this being part of the component vs part of the demo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had a similar thought as well, but had decided to make it internal in the end, just so that it was all in the same place. I'd initially added these fields to the actual properties, but decided to change that since doing it like this would also allow the user to choose the order of things rather than have them be alphabetical order or anything like that (since properties are usually in alphabetical order)

I'm not sure I understand what you mean by seeing the different types within the same demo though?

Copy link
Contributor

@BhamelinD2L BhamelinD2L Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I mentioned in our presentation, I think there's an argument to be made that showing varieties can have value sometimes, but specific configurations should be demo-adjustable.

For example, having a click me to do x button is utilitarian (Do you need this feature or not?), but deciding which type of alert, or if you need a normal vs subtle button, may be a softer problem that having choices on-screen at the same time can help solve.

With that in mind, something like this would be extremely cool:
image
But I don't think that's a problem we will solve for now :D

render() {
const hasActions = this.buttonText && this.buttonText.length > 0 || this.hasCloseButton;
const alertTextClasses = {
Expand Down
6 changes: 4 additions & 2 deletions components/alert/demo/alert.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ <h2>Default</h2>
</template>
</d2l-demo-snippet>

<h2>Success</h2>
<!-- In theory the demos below wouldn't be needed if we added everything -->

<!-- <h2>Success</h2>
<d2l-demo-snippet>
<template>
<d2l-alert type="success">A success message.</d2l-alert>
Expand Down Expand Up @@ -65,7 +67,7 @@ <h2>With Subtext</h2>
A message.
</d2l-alert>
</template>
</d2l-demo-snippet>
</d2l-demo-snippet> -->

</d2l-demo-page>

Expand Down
104 changes: 101 additions & 3 deletions components/demo/demo-snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import '../button/button-subtle.js';
import '../switch/switch.js';
import '../dropdown/dropdown.js';
import '../dropdown/dropdown-content.js';
import '../inputs/input-date.js';
import '../inputs/input-text.js';
import { css, html, LitElement } from 'lit';
import { labelStyles } from '../typography/styles.js';
import { selectStyles } from '../inputs/input-select-styles.js';

class DemoSnippet extends LitElement {

Expand All @@ -14,6 +18,7 @@ class DemoSnippet extends LitElement {
noPadding: { type: Boolean, reflect: true, attribute: 'no-padding' },
overflowHidden: { type: Boolean, reflect: true, attribute: 'overflow-hidden' },
_code: { type: String },
_controlProperties: { type: Array },
_fullscreen: { state: true },
_hasSkeleton: { type: Boolean, attribute: false },
_settingsPeek: { state: true },
Expand All @@ -22,7 +27,7 @@ class DemoSnippet extends LitElement {
}

static get styles() {
return css`
return [ labelStyles, selectStyles, css`
:host {
background-color: white;
border: 1px solid var(--d2l-color-tungsten);
Expand Down Expand Up @@ -106,7 +111,14 @@ class DemoSnippet extends LitElement {
:host([code-view-hidden]) d2l-code-view {
display: none;
}
`;
.d2l-demo-control {
display: block;
padding-top: 0.5rem;
}
.d2l-demo-control-dropdown {
width: 100%;
}
`];
}

constructor() {
Expand All @@ -121,7 +133,9 @@ class DemoSnippet extends LitElement {
const skeleton = this._hasSkeleton ? html`<d2l-switch text="Skeleton" ?on="${this._skeletonOn}" @change="${this._handleSkeletonChange}"></d2l-switch>` : null;
const switches = html`
<d2l-switch text="Fullscreen" ?on="${this._fullscreen}" @change="${this._handleFullscreenChange}"></d2l-switch><br />
${skeleton}`;
${skeleton}
${this._renderControls()}
`;
const settings = this.fullWidth && this._fullscreen ? html`
<d2l-dropdown class="settings-dropdown ${this._settingsPeek ? 'peek' : ''}">
<d2l-button-subtle primary icon="tier1:gear" text="Settings" class="d2l-dropdown-opener"></d2l-button-subtle>
Expand Down Expand Up @@ -215,6 +229,40 @@ class DemoSnippet extends LitElement {
.replace(/=""/g, ''); // replace empty strings for boolean attributes (="")
}

_getProperties() {
// Possible improvements:
// - Add more control types
// - Allow grouping things together to split things up more easily on the side bar (using styles)
// - Improve default value support (settings a value in the demo directly doesn't reflect in the control)

if (!this.shadowRoot) return;
const query = this._isTemplate ? 'slot[name="_demo"]' : 'slot:not([name="_demo"])';
const nodes = this.shadowRoot.querySelector(query).assignedNodes();

this._controlProperties = [];

const doApply = (nodes) => {
for (let i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType === Node.ELEMENT_NODE) {
if (nodes[i].constructor.demoProperties) {
Object.entries(nodes[i].constructor.demoProperties).forEach(([key, value]) => {
this._controlProperties.push({
label: value.label || key,
attributeName: value.attribute || key,
type: value.type,
options: value.options,
node: nodes[i]
});
});
}

doApply(nodes[i].children);
}
}
};
doApply(nodes);
}

async _handleFullscreenChange(e) {
this._fullscreen = e.target.on;
this._settingsPeek = this._fullscreen;
Expand All @@ -234,6 +282,14 @@ class DemoSnippet extends LitElement {
this._updateCode(e.target);
}

_onControlValueChange(e) {
this._updateDemoAttribute(e.target.controlObject, e.target.value);
}

_onSwitchControlChange(e) {
this._updateDemoAttribute(e.target.controlObject, e.target.on);
}

_removeImportedDemo() {
if (!this.shadowRoot) return;
const nodes = this.shadowRoot.querySelector('slot[name="_demo"]').assignedNodes();
Expand All @@ -242,6 +298,38 @@ class DemoSnippet extends LitElement {
}
}

_renderControls() {
if (!this._controlProperties?.length) return;

return this._controlProperties.map((control) => {
switch (control.type) {
case 'switch':
return html`<d2l-switch class="d2l-demo-control" text="${control.label}" .controlObject=${control} @change="${this._onSwitchControlChange}"></d2l-switch>`;
case 'select':
return html`
<div class="d2l-demo-control">
<label class="d2l-label-text">${control.label}</label>
<select class="d2l-input-select d2l-demo-control-dropdown" .controlObject=${control} aria-label=${control.label} @change=${this._onControlValueChange}>
${control.options.map(option => html`<option value="${option}">${option}</option>`)}
</select>
</div>
`;
case 'text':
return html`
<div class="d2l-demo-control">
<d2l-input-text label="${control.label}" .controlObject=${control} @change=${this._onControlValueChange}></d2l-input-text>
</div>
`;
case 'date':
return html`
<div class="d2l-demo-control">
<d2l-input-date label="${control.label}" .controlObject=${control} @change=${this._onControlValueChange}></d2l-input-date>
</div>
`;
}
});
}

_repeat(value, times) {
if (!value || !times) return '';
if (!''.repeat) return Array(times).join(value); // for IE11
Expand Down Expand Up @@ -273,6 +361,16 @@ class DemoSnippet extends LitElement {
this._code = textNode.textContent;

this._updateHasSkeleton();
this._getProperties();
}

_updateDemoAttribute(control, value) {
if (value) {
control.node.setAttribute(control.attributeName, value);
} else {
control.node.removeAttribute(control.attributeName);
}
this._code = control.node.outerHTML;
}

_updateHasSkeleton() {
Expand Down
3 changes: 3 additions & 0 deletions components/status-indicator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,6 @@ The text label should be kept short; one or two words at most. If more informati

</div>
```

## Accessibility
Although `aria-description` can be used to provide additional context on the state, it is recommended to provide that context for both sighted and non-sighted users by following the recommendations mentioned in the [Content](#content) section
2 changes: 1 addition & 1 deletion components/status-indicator/status-indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StatusIndicator extends LitElement {
reflect: true
},
/**
* REQUIRED: The text that is displayed within the status indicator
* ACCESSIBILITY: REQUIRED: The text that is displayed within the status indicator
* @type {string}
*/
text: {
Expand Down
Loading