Skip to content

Commit

Permalink
create function to show/hide the options panel
Browse files Browse the repository at this point in the history
The new function is available globally as ytaf_showOptionsPanel() for
console use.
  • Loading branch information
throwaway96 committed Mar 25, 2024
1 parent 30d8192 commit fefc326
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ function createOptionsPanel() {
document.querySelector(':focus').click();
} else if (evt.keyCode === 27) {
// Back button
elmContainer.style.display = 'none';
elmContainer.blur();
showOptionsPanel(false);
}

evt.preventDefault();
Expand Down Expand Up @@ -126,6 +125,30 @@ function createOptionsPanel() {
const optionsPanel = createOptionsPanel();
document.body.appendChild(optionsPanel);

let optionsPanelVisible = false;

/**
* Show or hide the options panel.
* @param {boolean} [visible=true] Whether to show the options panel.
*/
function showOptionsPanel(visible) {
visible ??= true;

if (visible && !optionsPanelVisible) {
console.info('Showing and focusing options panel!');
optionsPanel.style.display = 'block';
optionsPanel.focus();
optionsPanelVisible = true;
} else if (!visible && optionsPanelVisible) {
console.info('Hiding options panel!');
optionsPanel.style.display = 'none';
optionsPanel.blur();
optionsPanelVisible = false;
}
}

window.ytaf_showOptionsPanel = showOptionsPanel;

const eventHandler = (evt) => {
console.info(
'Key event:',
Expand All @@ -142,15 +165,8 @@ const eventHandler = (evt) => {
evt.stopPropagation();

if (evt.type === 'keydown') {
if (optionsPanel.style.display === 'none') {
console.info('Showing and focusing!');
optionsPanel.style.display = 'block';
optionsPanel.focus();
} else {
console.info('Hiding!');
optionsPanel.style.display = 'none';
optionsPanel.blur();
}
// Toggle visibility.
showOptionsPanel(!optionsPanelVisible);
}
return false;
}
Expand Down

0 comments on commit fefc326

Please sign in to comment.