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

feat: migrate d2l-labs-view-toggle #467

Merged
merged 6 commits into from
Dec 12, 2024
Merged
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
25 changes: 25 additions & 0 deletions demo/components/view-toggle/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/node_modules/@brightspace-ui/core/components/demo/styles.css" type="text/css">
<script type="module">
import '@brightspace-ui/core/components/demo/demo-page.js';
import '../../../src/components/view-toggle/view-toggle.js';
</script>
<title>d2l-cpd</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
</head>
<body>
<d2l-demo-page page-title="d2l-labs-view-toggle">
<h2>d2l-cpd</h2>
<d2l-demo-snippet>
<d2l-labs-view-toggle
toggle-options='[{"text":"Bananas","val":"overall"},{"text":"Minions","val":"minios"},{"text":"Pyjamas","val":"subject"}]'
text="Toggle: "
>
</d2l-labs-view-toggle>
</d2l-demo-snippet>
</d2l-demo-page>
</body>
</html>
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h2 class="d2l-heading-2">Components</h2>
<li><a href="demo/components/community/index.html">Community</a></li>
<li><a href="demo/components/opt-in-flyout/index.html">Opt-In Flyout</a></li>
<li><a href="demo/components/pagination/index.html">Pagination</a></li>
<li><a href="demo/components/view-toggle/index.html">View Toggle</a></li>
</ul>

<h2 class="d2l-heading-3">Controllers</h2>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"./components/opt-out-flyout.js": "./src/components/opt-in-flyout/opt-out-flyout.js",
"./components/opt-out-reason.js": "./src/components/opt-in-flyout/opt-out-reason.js",
"./components/pager-numeric.js": "./src/components/pagination/pager-numeric.js",
"./components/view-toggle.js": "./src/components/view-toggle/view-toggle.js",
"./controllers/computed-value.js": "./src/controllers/computed-values/computed-value.js",
"./controllers/computed-values.js": "./src/controllers/computed-values/computed-values.js",
"./controllers/language-listener.js": "./src/controllers/language-listener/language-listener.js"
Expand Down
15 changes: 15 additions & 0 deletions src/components/view-toggle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# d2l-labs-view-toggle

A Lit element component for toggling between views.

## Usage

```html
<script type="module">
import '@brightspace-ui-labs/view-toggle/view-toggle.js';
</script>
<d2l-labs-view-toggle
toggle-options='[{"text":"Bananas","val":"overall"},{"text":"Minions","val":"minios"},{"text":"Pyjamas","val":"subject"}]'
text="Toggle: "
></d2l-labs-view-toggle>
```
143 changes: 143 additions & 0 deletions src/components/view-toggle/view-toggle.js
Copy link
Member

Choose a reason for hiding this comment

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

You'll need to add this file to the exports:
https://github.com/BrightspaceUI/labs/blob/main/package.json#L6

Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import '@brightspace-ui/core/components/icons/icon.js';
import '@brightspace-ui/core/components/colors/colors.js';
import { css, html, LitElement } from 'lit';

class ViewToggle extends LitElement {
static get properties() {
return {
text: {
type: String
},
toggleOptions: {
dlockhart marked this conversation as resolved.
Show resolved Hide resolved
type: Array,
attribute: 'toggle-options'
},
selectedOption: {
type: String,
attribute: 'selected-options'
}
};
}
static get styles() {
return [
css`
button.d2l-labs-view-toggle-left {
border-end-start-radius: 0.3rem;
border-inline-end-color: transparent;
border-inline-start-color: var(--d2l-color-mica);
border-start-start-radius: 0.3rem;
}
button.d2l-labs-view-toggle-right {
border-end-end-radius: 0.3rem;
border-inline-end-color: var(--d2l-color-mica);
border-inline-start-color: transparent;
border-start-end-radius: 0.3rem;
}
button {
background-color: var(--d2l-color-sylvite);
border-color: var(--d2l-color-mica);
border-style: solid;
border-width: 1px;
box-sizing: border-box;
color: var(--d2l-color-ferrite);
cursor: pointer;
display: inline;
flex: 1;
font-family: inherit;
font-size: 0.7rem;
font-weight: 700;
margin: 0;
min-height: calc(2rem + 2px);
outline: none;
padding: 0.5rem 1.5rem;
text-align: center;
transition: box-shadow 0.2s;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
vertical-align: middle;
white-space: nowrap;
}
button:hover, button:focus {
border: 1px solid var(--d2l-color-celestine) !important;
}
button[aria-pressed="true"] {
background-color: var(--d2l-color-tungsten);
border-color: var(--d2l-color-tungsten);
color: var(--d2l-color-regolith);
}
button[aria-pressed="true"]:hover, button[aria-pressed="true"]:focus {
box-shadow: inset 0 0 0 2px #ffffff;
}
:host {
display: flex;
width: 100%;
}
.view-toggle-container {
display: none;
}
@media (min-width: 525px) {
:host {
display: block;
margin: 0 -0.9rem;
width: auto;
}
.view-toggle-container {
display: inline;
margin: 0 0.9rem;
}
}`
];
}

render() {
return html`
<div class="view-toggle-container">
<span>${this.text}</span>
${this.toggleOptions?.map((option, i) => this.#renderButton(option, i))}
</div>
`;
}

willUpdate(changedProperties) {
super.willUpdate(changedProperties);
if (!this.selectedOption && changedProperties.has('toggleOptions')) {
this.selectedOption = this.toggleOptions[0].val;
}
}
#isSelected(option) {
return option.val === this.selectedOption;
}
#renderButton(option, index) {
let placement = 'centre';
if (index === 0) {
placement = 'left';
}
if (this.toggleOptions && index === this.toggleOptions.length - 1) {
placement = 'right';
}
return html`<button
data-option-val="${option.val}"
aria-pressed="${this.#isSelected(option)}"
class="d2l-labs-view-toggle-${placement}"
@click="${this.#selectIndex}"
>${option.text}</button>`;
}
#selectIndex(e) {
this.selectedOption = e.target.dataset.optionVal;
this.dispatchEvent(
new CustomEvent(
'd2l-labs-view-toggle-changed',
{
detail: {
view: this.selectedOption
},
composed: true,
bubbles: true
}
)
);
}
}
customElements.define('d2l-labs-view-toggle', ViewToggle);
18 changes: 18 additions & 0 deletions test/components/view-toggle/view-toggle.axe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import '../../../src/components/view-toggle/view-toggle.js';
import { expect, fixture, html } from '@brightspace-ui/testing';

const basicFixture = html`<d2l-labs-view-toggle
toggle-options='[{"text":"Bananas","val":"overall"},{"text":"Minions","val":"minios"},{"text":"Pyjamas","val":"subject"}]'
text="Toggle: ">
</d2l-labs-view-toggle>`;

describe('d2l-labs-view-toggle', () => {

describe('accessibility', () => {
it('should pass all aXe tests', async() => {
const el = await fixture(basicFixture);
await expect(el).to.be.accessible();
});
});

});
26 changes: 26 additions & 0 deletions test/components/view-toggle/view-toggle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '../../../src/components/view-toggle/view-toggle.js';
import { clickElem, expect, fixture, html, oneEvent } from '@brightspace-ui/testing';
import { runConstructor } from '@brightspace-ui/core/tools/constructor-test-helper.js';
Comment on lines +2 to +3
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import { clickElem, expect, fixture, html, oneEvent } from '@brightspace-ui/testing';
import { runConstructor } from '@brightspace-ui/core/tools/constructor-test-helper.js';
import { clickElem, expect, fixture, html, oneEvent, runConstructor } from '@brightspace-ui/testing';

Copy link
Member

Choose a reason for hiding this comment

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

This suggestion is still open. :)


const basicFixture = html`<d2l-labs-view-toggle
toggle-options='[{"text":"Bananas","val":"overall"},{"text":"Minions","val":"minios"},{"text":"Pyjamas","val":"subject"}]'
text="Toggle: "></d2l-labs-view-toggle>`;

describe('d2l-labs-view-toggle', () => {

describe('constructor', () => {
it('should construct', () => {
runConstructor('d2l-labs-view-toggle');
});
});

describe('events', () => {
it('should fire toggle changed event when the button is clicked', async() => {
const el = await fixture(basicFixture);
clickElem(el.shadowRoot.querySelector('button[data-option-val=subject]'));
const e = await oneEvent(el, 'd2l-labs-view-toggle-changed');
expect(e.detail.view).to.equal('subject');
});
});

});
Loading