Skip to content

Commit

Permalink
fix(player): watch menu button hint part changes
Browse files Browse the repository at this point in the history
closes #1102
  • Loading branch information
mihar-22 committed Feb 15, 2024
1 parent 9bfa26e commit a17239a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
4 changes: 3 additions & 1 deletion packages/vidstack/mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -776,5 +776,7 @@
"_saveLang": "tn",
"_selectTracks": "un",
"_watchSwipeGesture": "xn",
"_appendSource": "yn"
"_appendSource": "yn",
"_hintEl": "zn",
"_watchHintEl": "An"
}
6 changes: 3 additions & 3 deletions packages/vidstack/src/components/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ export class MediaProvider extends Component<MediaProviderProps, MediaProviderSt
const resize = new ResizeObserver(animationFrameThrottle(this._onResize.bind(this)));
resize.observe(el);

const mutation = new MutationObserver(this._onMutation.bind(this));
mutation.observe(el, { attributes: true, childList: true });
const mutations = new MutationObserver(this._onMutation.bind(this));
mutations.observe(el, { attributes: true, childList: true });

this._onResize();
this._onMutation();

onDispose(() => {
resize.disconnect();
mutation.disconnect();
mutations.disconnect();
});
}

Expand Down
30 changes: 22 additions & 8 deletions packages/vidstack/src/components/ui/menu/menu-button.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, effect, prop, useContext } from 'maverick.js';
import { Component, effect, onDispose, prop, signal, useContext } from 'maverick.js';
import { DOMEvent } from 'maverick.js/std';

import { FocusVisibleController } from '../../../foundation/observers/focus-visible';
Expand All @@ -20,6 +20,7 @@ export class MenuButton extends Component<MenuButtonProps, {}, MenuButtonEvents>
};

private _menu!: MenuContext;
private _hintEl = signal<HTMLElement | null>(null);

@prop
get expanded() {
Expand All @@ -42,13 +43,12 @@ export class MenuButton extends Component<MenuButtonProps, {}, MenuButtonEvents>
}

protected override onConnect(el: HTMLElement) {
const hint = Array.from(el.querySelectorAll('[data-part="hint"]')).pop();
if (hint) {
effect(() => {
const text = this._menu._hint();
if (text) hint.textContent = text;
});
}
effect(this._watchHintEl.bind(this));

this._onMutation();
const mutations = new MutationObserver(this._onMutation.bind(this));
mutations.observe(el, { attributeFilter: ['data-part'], childList: true, subtree: true });
onDispose(() => mutations.disconnect());

onPress(el, (trigger) => {
this.dispatch('select', { trigger });
Expand All @@ -58,6 +58,20 @@ export class MenuButton extends Component<MenuButtonProps, {}, MenuButtonEvents>
private _watchDisabled() {
this._menu._disableMenuButton(this.$props.disabled());
}

private _watchHintEl() {
const el = this._hintEl();
if (!el) return;
effect(() => {
const text = this._menu._hint();
if (text) el.textContent = text;
});
}

private _onMutation() {
const hintEl = this.el?.querySelector<HTMLElement>('[data-part="hint"]');
this._hintEl.set(hintEl ?? null);
}
}

export interface MenuButtonProps {
Expand Down

0 comments on commit a17239a

Please sign in to comment.