feat(core): add pip button component (#525)

This commit is contained in:
rahim
2026-02-13 22:21:09 +11:00
committed by GitHub
parent 7b41930eed
commit 2c8b77af45
9 changed files with 342 additions and 0 deletions
@@ -0,0 +1,64 @@
import { PiPButtonCore, PiPButtonDataAttrs } from '@videojs/core';
import { applyElementProps, applyStateDataAttrs, createButton, logMissingFeature, selectPiP } from '@videojs/core/dom';
import type { PropertyDeclarationMap, PropertyValues } from '@videojs/element';
import { playerContext } from '../../player/context';
import { PlayerController } from '../../player/player-controller';
import { MediaElement } from '../media-element';
export class PiPButtonElement extends MediaElement {
static readonly tagName = 'media-pip-button';
static override properties = {
label: { type: String },
disabled: { type: Boolean },
} satisfies PropertyDeclarationMap<keyof PiPButtonCore.Props>;
label = PiPButtonCore.defaultProps.label;
disabled = PiPButtonCore.defaultProps.disabled;
readonly #core = new PiPButtonCore();
readonly #state = new PlayerController(this, playerContext, selectPiP);
#disconnect: AbortController | null = null;
override connectedCallback(): void {
super.connectedCallback();
this.#disconnect = new AbortController();
const buttonProps = createButton({
onActivate: () => this.#core.toggle(this.#state.value!),
isDisabled: () => this.disabled || !this.#state.value,
});
applyElementProps(this, buttonProps, this.#disconnect.signal);
if (__DEV__ && !this.#state.value) {
logMissingFeature(PiPButtonElement.tagName, 'pip');
}
}
override disconnectedCallback(): void {
super.disconnectedCallback();
this.#disconnect?.abort();
this.#disconnect = null;
}
protected override willUpdate(changed: PropertyValues): void {
super.willUpdate(changed);
this.#core.setProps(this);
}
protected override update(changed: PropertyValues): void {
super.update(changed);
const media = this.#state.value;
if (!media) return;
const state = this.#core.getState(media);
applyElementProps(this, this.#core.getAttrs(state));
applyStateDataAttrs(this, state, PiPButtonDataAttrs);
}
}