Skip to content

Commit

Permalink
fix: add indicator CSS parts
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Jul 27, 2023
1 parent 9bdc3f5 commit 83f8f9c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 87 deletions.
76 changes: 37 additions & 39 deletions src/js/experimental/media-captions-listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,6 @@ slotTemplate.innerHTML = /*html*/`
<slot hidden name="captions-indicator">${ccIcon}</slot>
`;

/**
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @returns {Array<Object>} An array of TextTrack-like objects.
*/
const getSubtitlesListAttr = (el, attrName) => {
const attrVal = el.getAttribute(attrName);
return attrVal ? parseTextTracksStr(attrVal) : [];
};

/**
*
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @param {Array<Object>} list An array of TextTrack-like objects
*/
const setSubtitlesListAttr = (el, attrName, list) => {
// null, undefined, and empty arrays are treated as "no value" here
if (!list?.length) {
el.removeAttribute(attrName);
return;
}

// don't set if the new value is the same as existing
const newValStr = stringifyTextTrackList(list);
const oldVal = el.getAttribute(attrName);
if (oldVal === newValStr) return;

el.setAttribute(attrName, newValStr);
};

/**
* @attr {string} mediasubtitleslist - (read-only) A list of all subtitles and captions.
* @attr {boolean} mediasubtitlesshowing - (read-only) A list of the showing subtitles and captions.
Expand Down Expand Up @@ -75,11 +44,13 @@ class MediaCaptionsListbox extends MediaChromeListbox {
constructor() {
super({ slotTemplate });

this.#selectedIndicator = this.getSlottedIndicator('selected-indicator');
this.#captionsIndicator = this.getSlottedIndicator('captions-indicator');
this.#selectedIndicator = this.getSlottedIndicator('selected');
this.#captionsIndicator = this.getSlottedIndicator('captions');

Check warning on line 48 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L47-L48

Added lines #L47 - L48 were not covered by tests
}

attributeChangedCallback(attrName, oldValue, newValue) {
super.attributeChangedCallback(attrName, oldValue, newValue);

Check warning on line 52 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L52

Added line #L52 was not covered by tests

if (attrName === MediaUIAttributes.MEDIA_SUBTITLES_LIST && oldValue !== newValue) {

Check warning on line 54 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L54

Added line #L54 was not covered by tests
this.#render();

Expand All @@ -91,8 +62,6 @@ class MediaCaptionsListbox extends MediaChromeListbox {
this.removeAttribute('aria-multiselectable');
console.warn("Captions List doesn't currently support multiple selections. You can enable multiple items via the media.textTrack API.");
}

super.attributeChangedCallback(attrName, oldValue, newValue);
}

connectedCallback() {
Expand Down Expand Up @@ -165,25 +134,54 @@ class MediaCaptionsListbox extends MediaChromeListbox {
}

#onChange() {
const selectedOption = this.selectedOptions[0]?.value;

// turn off currently selected tracks
toggleSubsCaps(this, false);

if (!selectedOption) return;
if (!this.value) return;

Check warning on line 140 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L140

Added line #L140 was not covered by tests

const event = new globalThis.CustomEvent(
MediaUIEvents.MEDIA_SHOW_SUBTITLES_REQUEST,
{
composed: true,
bubbles: true,
detail: selectedOption,
detail: this.value,

Check warning on line 147 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L147

Added line #L147 was not covered by tests
}
);
this.dispatchEvent(event);
}
}

/**
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @returns {Array<Object>} An array of TextTrack-like objects.
*/
const getSubtitlesListAttr = (el, attrName) => {
const attrVal = el.getAttribute(attrName);
return attrVal ? parseTextTracksStr(attrVal) : [];

Check warning on line 161 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L160-L161

Added lines #L160 - L161 were not covered by tests
};

/**
*
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @param {Array<Object>} list An array of TextTrack-like objects
*/
const setSubtitlesListAttr = (el, attrName, list) => {
// null, undefined, and empty arrays are treated as "no value" here
if (!list?.length) {
el.removeAttribute(attrName);
return;
}

// don't set if the new value is the same as existing
const newValStr = stringifyTextTrackList(list);
const oldVal = el.getAttribute(attrName);
if (oldVal === newValStr) return;

el.setAttribute(attrName, newValStr);

Check warning on line 182 in src/js/experimental/media-captions-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-listbox.js#L171-L182

Added lines #L171 - L182 were not covered by tests
};

if (!globalThis.customElements.get('media-captions-listbox')) {
globalThis.customElements.define('media-captions-listbox', MediaCaptionsListbox);
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/experimental/media-captions-selectmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MediaCaptionsSelectMenu extends MediaChromeSelectMenu {

const captionsListbox = document.createElement('media-captions-listbox');
captionsListbox.part.add('listbox');
captionsListbox.setAttribute('exportparts', 'option');
captionsListbox.setAttribute('exportparts', 'option, indicator');

Check warning on line 19 in src/js/experimental/media-captions-selectmenu.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-captions-selectmenu.js#L19

Added line #L19 was not covered by tests

const buttonSlot = this.shadowRoot.querySelector('slot[name=button]');
const listboxSlot = this.shadowRoot.querySelector('slot[name=listbox]');
Expand Down
12 changes: 6 additions & 6 deletions src/js/experimental/media-chrome-listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ template.innerHTML = /*html*/`
margin-inline: .5ch;
}
media-chrome-option > .indicator {
media-chrome-option > [part~="indicator"] {
fill: var(--media-icon-color, var(--media-primary-color, rgb(238 238 238)));
height: var(--media-option-indicator-height, 1.25em);
vertical-align: var(--media-option-indicator-vertical-align, text-top);
}
media-chrome-option > .selected-indicator {
media-chrome-option > [part~="selected"] {
margin-top: -.06em;
}
media-chrome-option[aria-selected="false"] > .selected-indicator {
media-chrome-option[aria-selected="false"] > [part~="selected"] {
visibility: hidden;
}
</style>
Expand Down Expand Up @@ -152,18 +152,18 @@ class MediaChromeListbox extends globalThis.HTMLElement {
}

Check warning on line 152 in src/js/experimental/media-chrome-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-chrome-listbox.js#L150-L152

Added lines #L150 - L152 were not covered by tests

getSlottedIndicator(name) {
let indicator = this.querySelector(`:scope > [slot="${name}"]`);
let indicator = this.querySelector(`:scope > [slot="${name}-indicator"]`);

// Chaining slots
if (indicator?.nodeName == 'SLOT')
// @ts-ignore
indicator = indicator.assignedElements({ flatten: true })[0];

if (!indicator)
indicator = this.shadowRoot.querySelector(`[name="${name}"] > svg`);
indicator = this.shadowRoot.querySelector(`[name="${name}-indicator"] > svg`);

indicator.removeAttribute('slot');
indicator.classList.add('indicator', name);
indicator.part.add(name, 'indicator');

return indicator;
}

Check warning on line 169 in src/js/experimental/media-chrome-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-chrome-listbox.js#L155-L169

Added lines #L155 - L169 were not covered by tests
Expand Down
8 changes: 3 additions & 5 deletions src/js/experimental/media-playback-rate-listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MediaPlaybackRateListbox extends MediaChromeListbox {
constructor() {
super();

this.#selectedIndicator = this.getSlottedIndicator('selected-indicator');
this.#selectedIndicator = this.getSlottedIndicator('selected');
this.#render();

Check warning on line 35 in src/js/experimental/media-playback-rate-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-playback-rate-listbox.js#L32-L35

Added lines #L32 - L35 were not covered by tests
}

Expand Down Expand Up @@ -104,16 +104,14 @@ class MediaPlaybackRateListbox extends MediaChromeListbox {
}

#onChange() {
const selectedOption = this.selectedOptions[0]?.value;

if (!selectedOption) return;
if (!this.value) return;

Check warning on line 107 in src/js/experimental/media-playback-rate-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-playback-rate-listbox.js#L107

Added line #L107 was not covered by tests

const event = new globalThis.CustomEvent(
MediaUIEvents.MEDIA_PLAYBACK_RATE_REQUEST,
{
composed: true,
bubbles: true,
detail: selectedOption,
detail: this.value,

Check warning on line 114 in src/js/experimental/media-playback-rate-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-playback-rate-listbox.js#L114

Added line #L114 was not covered by tests
}
);
this.dispatchEvent(event);
Expand Down
2 changes: 1 addition & 1 deletion src/js/experimental/media-playback-rate-selectmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MediaPlaybackRateSelectMenu extends MediaChromeSelectMenu {

const playbackRateListbox = document.createElement('media-playback-rate-listbox');
playbackRateListbox.part.add('listbox');
playbackRateListbox.setAttribute('exportparts', 'option');
playbackRateListbox.setAttribute('exportparts', 'option, indicator');

Check warning on line 19 in src/js/experimental/media-playback-rate-selectmenu.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-playback-rate-selectmenu.js#L17-L19

Added lines #L17 - L19 were not covered by tests

const buttonSlot = this.shadowRoot.querySelector('slot[name=button]');
const listboxSlot = this.shadowRoot.querySelector('slot[name=listbox]');
Expand Down
5 changes: 2 additions & 3 deletions src/js/experimental/media-rendition-listbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class MediaRenditionListbox extends MediaChromeListbox {
constructor() {
super();

Check warning on line 29 in src/js/experimental/media-rendition-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-rendition-listbox.js#L28-L29

Added lines #L28 - L29 were not covered by tests

this.#selectedIndicator = this.getSlottedIndicator('selected-indicator');
this.#selectedIndicator = this.getSlottedIndicator('selected');

Check warning on line 31 in src/js/experimental/media-rendition-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-rendition-listbox.js#L31

Added line #L31 was not covered by tests
}

attributeChangedCallback(attrName, oldValue, newValue) {
super.attributeChangedCallback(attrName, oldValue, newValue);

Check warning on line 35 in src/js/experimental/media-rendition-listbox.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-rendition-listbox.js#L35

Added line #L35 was not covered by tests

if (attrName === MediaUIAttributes.MEDIA_RENDITION_SELECTED && oldValue !== newValue) {
this.value = newValue ?? 'auto';
Expand All @@ -41,8 +42,6 @@ class MediaRenditionListbox extends MediaChromeListbox {
this.#renditionList = parseRenditionList(newValue);
this.#render();
}

super.attributeChangedCallback(attrName, oldValue, newValue);
}

connectedCallback() {
Expand Down
2 changes: 1 addition & 1 deletion src/js/experimental/media-rendition-selectmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MediaRenditionSelectMenu extends MediaChromeSelectMenu {

const renditionListbox = document.createElement('media-rendition-listbox');
renditionListbox.part.add('listbox');
renditionListbox.setAttribute('exportparts', 'option');
renditionListbox.setAttribute('exportparts', 'option, indicator');

Check warning on line 19 in src/js/experimental/media-rendition-selectmenu.js

View check run for this annotation

Codecov / codecov/patch

src/js/experimental/media-rendition-selectmenu.js#L19

Added line #L19 was not covered by tests

const buttonSlot = this.shadowRoot.querySelector('slot[name=button]');
const listboxSlot = this.shadowRoot.querySelector('slot[name=listbox]');
Expand Down
62 changes: 31 additions & 31 deletions src/js/media-captions-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,6 @@ const updateAriaChecked = (el) => {
el.setAttribute('aria-checked', areSubsOn(el));
};

/**
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @returns {Array<Object>} An array of TextTrack-like objects.
*/
const getSubtitlesListAttr = (el, attrName) => {
const attrVal = el.getAttribute(attrName);
return attrVal ? parseTextTracksStr(attrVal) : [];
};

/**
*
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @param {Array<Object>} list An array of TextTrack-like objects
*/
const setSubtitlesListAttr = (el, attrName, list) => {
// null, undefined, and empty arrays are treated as "no value" here
if (!list?.length) {
el.removeAttribute(attrName);
return;
}

// don't set if the new value is the same as existing
const newValStr = stringifyTextTrackList(list);
const oldVal = el.getAttribute(attrName);
if (oldVal === newValStr) return;

el.setAttribute(attrName, newValStr);
};

/**
* @slot on - An element that will be shown while closed captions or subtitles are on.
* @slot off - An element that will be shown while closed captions or subtitles are off.
Expand Down Expand Up @@ -144,6 +113,37 @@ class MediaCaptionsButton extends MediaChromeButton {
}
}

/**
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @returns {Array<Object>} An array of TextTrack-like objects.
*/
const getSubtitlesListAttr = (el, attrName) => {
const attrVal = el.getAttribute(attrName);
return attrVal ? parseTextTracksStr(attrVal) : [];

Check warning on line 123 in src/js/media-captions-button.js

View check run for this annotation

Codecov / codecov/patch

src/js/media-captions-button.js#L122-L123

Added lines #L122 - L123 were not covered by tests
};

/**
*
* @param {any} el Should be HTMLElement but issues with globalThis shim
* @param {string} attrName
* @param {Array<Object>} list An array of TextTrack-like objects
*/
const setSubtitlesListAttr = (el, attrName, list) => {
// null, undefined, and empty arrays are treated as "no value" here
if (!list?.length) {
el.removeAttribute(attrName);
return;
}

// don't set if the new value is the same as existing
const newValStr = stringifyTextTrackList(list);
const oldVal = el.getAttribute(attrName);
if (oldVal === newValStr) return;

el.setAttribute(attrName, newValStr);

Check warning on line 144 in src/js/media-captions-button.js

View check run for this annotation

Codecov / codecov/patch

src/js/media-captions-button.js#L133-L144

Added lines #L133 - L144 were not covered by tests
};

if (!globalThis.customElements.get('media-captions-button')) {
globalThis.customElements.define('media-captions-button', MediaCaptionsButton);
}
Expand Down

0 comments on commit 83f8f9c

Please sign in to comment.