-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8182825
feat: migrate d2l-labs-view-toggle
GZolla 990b81c
fix demo import
GZolla d9e99b9
From review
GZolla 6937e32
Appease Linter
GZolla 571c636
Add export
GZolla 6699c21
Merge branch 'main' into gzolla/migrate-view-toggle
GZolla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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> |
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
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,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> | ||
``` |
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,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); |
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,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(); | ||
}); | ||
}); | ||
|
||
}); |
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,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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||||||||
}); | ||||||||
}); | ||||||||
|
||||||||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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