Skip to content

Commit

Permalink
add keyboard support to lobby pools
Browse files Browse the repository at this point in the history
  • Loading branch information
blevantovych committed Oct 27, 2024
1 parent fdef02d commit 8339b05
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions ui/lobby/src/view/pools.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { h, Hooks } from 'snabbdom';
import { h, Hooks, VNode } from 'snabbdom';
import { spinnerVdom as spinner } from 'common/spinner';
import { bind } from 'common/snabbdom';
import LobbyController from '../ctrl';

export const hooks = (ctrl: LobbyController): Hooks =>
bind(
'click',
e => {
if (ctrl.redirecting) return;
const id =
(e.target as HTMLElement).dataset['id'] ||
((e.target as HTMLElement).parentNode as HTMLElement).dataset['id'];
if (id === 'custom') ctrl.setupCtrl.openModal('hook');
else if (id) ctrl.clickPool(id);
const createHandler = (ctrl: LobbyController) => (e: Event) => {
if (ctrl.redirecting) return;

if (e instanceof KeyboardEvent) {
if (e.key !== 'Enter' && e.key !== ' ') return;
e.preventDefault(); // Prevent page scroll on space
}

const id =
(e.target as HTMLElement).dataset['id'] ||
((e.target as HTMLElement).parentNode as HTMLElement).dataset['id'];
if (id === 'custom') ctrl.setupCtrl.openModal('hook');
else if (id) ctrl.clickPool(id);

ctrl.redraw();
};

export const hooks = (ctrl: LobbyController): Hooks => {
const handler = createHandler(ctrl);
return {
insert: (vnode: VNode) => {
const el = vnode.elm as HTMLElement;
el.addEventListener('click', handler);
el.addEventListener('keydown', handler);
},
ctrl.redraw,
);
destroy: (vnode: VNode) => {
const el = vnode.elm as HTMLElement;
el.removeEventListener('click', handler);
el.removeEventListener('keydown', handler);
},
};
};

export function render(ctrl: LobbyController) {
const member = ctrl.poolMember;
Expand All @@ -27,7 +45,7 @@ export function render(ctrl: LobbyController) {
'div',
{
class: { active, transp },
attrs: { role: 'button', 'data-id': pool.id },
attrs: { role: 'button', 'data-id': pool.id, tabindex: '0' },
},
[
h('div.clock', `${pool.lim}+${pool.inc}`),
Expand All @@ -41,7 +59,10 @@ export function render(ctrl: LobbyController) {
.concat(
h(
'div.custom',
{ class: { transp: !!member }, attrs: { role: 'button', 'data-id': 'custom' } },
{
class: { transp: !!member },
attrs: { role: 'button', 'data-id': 'custom', tabindex: '0' },
},
i18n.site.custom,
),
);
Expand Down

0 comments on commit 8339b05

Please sign in to comment.