Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tooltip for new items #52

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export enum ATTRIBUTE {

export enum EVENT {
MOUSEDOWN = 'mousedown',
MOUSEENTER = 'mouseenter',
MOUSELEAVE = 'mouseleave',
KEYDOWN = 'keydown',
HASS_EDIT_SIDEBAR = 'hass-edit-sidebar'
}
Expand Down
44 changes: 41 additions & 3 deletions src/custom-sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ConfigOrderWithItem,
HassObject,
PartialPanelResolver,
PaperListBox,
Sidebar,
Match,
SuscriberEvent,
RenderTextParams
Expand Down Expand Up @@ -70,6 +70,8 @@ class CustomSidebar {
this._titleEntities = new Set<string>();
this._sidebarScroll = 0;
this._itemTouchedBinded = this._itemTouched.bind(this);
this._mouseEnterBinded = this._mouseEnter.bind(this);
this._mouseLeaveBinded = this._mouseLeave.bind(this);
this._configPromise = fetchConfig();
this._process();
}
Expand All @@ -86,6 +88,8 @@ class CustomSidebar {
private _titleEntities: Set<string>;
private _items: ConfigOrderWithItem[];
private _itemTouchedBinded: () => Promise<void>;
private _mouseEnterBinded: (event: MouseEvent) => void;
private _mouseLeaveBinded: () => void;

private async _getOrder(): Promise<ConfigOrder[]> {
const device = this._getCurrentDevice();
Expand Down Expand Up @@ -692,6 +696,14 @@ class CustomSidebar {

if (!orderItem.hide) {

if (orderItem.new_item) {

// New items rollover
orderItem.element.addEventListener(EVENT.MOUSEENTER, this._mouseEnterBinded);
orderItem.element.addEventListener(EVENT.MOUSELEAVE, this._mouseLeaveBinded);

}

// When the item is clicked
orderItem.element.addEventListener(EVENT.MOUSEDOWN, this._itemTouchedBinded);
orderItem.element.addEventListener(EVENT.KEYDOWN, (event: KeyboardEvent): void => {
Expand All @@ -717,17 +729,43 @@ class CustomSidebar {

private async _itemTouched(): Promise<void> {
this._sidebar.selector.$.query(ELEMENT.PAPER_LISTBOX).element
.then((paperListBox: PaperListBox): void => {
.then((paperListBox: HTMLElement): void => {
this._sidebarScroll = paperListBox.scrollTop;
});
}

private _mouseEnter(event: MouseEvent): void {
this._sidebar.element
.then((sidebar: Sidebar): void => {
if (sidebar.alwaysExpand) {
return;
}
if (sidebar._mouseLeaveTimeout) {
clearTimeout(sidebar._mouseLeaveTimeout);
sidebar._mouseLeaveTimeout = undefined;
}
sidebar._showTooltip(event.currentTarget as HTMLAnchorElement);
});
}

private async _mouseLeave(): Promise<void> {
this._sidebar.element
.then((sidebar: Sidebar): void => {
if (sidebar._mouseLeaveTimeout) {
clearTimeout(sidebar._mouseLeaveTimeout);
}
sidebar._mouseLeaveTimeout = window.setTimeout(() => {
sidebar._hideTooltip();
}, 500);
});
}

private async _panelLoaded(): Promise<void> {

// Select the right element in the sidebar
const panelResolver = await this._partialPanelResolver.element as PartialPanelResolver;
const pathName = panelResolver.__route.path;
const paperListBox = await this._sidebar.selector.$.query(ELEMENT.PAPER_LISTBOX).element as PaperListBox;
const paperListBox = await this._sidebar.selector.$.query(ELEMENT.PAPER_LISTBOX).element as HTMLElement;
const activeLink = paperListBox.querySelector<HTMLAnchorElement>(
[
`${SELECTOR.SCOPE} > ${SELECTOR.ITEM}[href="${pathName}"]`,
Expand Down
7 changes: 5 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export interface PartialPanelResolver extends HTMLElement {
}
}

export interface PaperListBox extends HTMLElement {
_updateAttrForSelected: () => void;
export interface Sidebar extends HTMLElement {
alwaysExpand: boolean;
_mouseLeaveTimeout?: number;
_showTooltip: (anchor: HTMLAnchorElement) => void;
_hideTooltip: () => void;
}

export enum Match {
Expand Down
22 changes: 22 additions & 0 deletions tests/05 - interactions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,26 @@ test('Pressing tab without being in the sidebar will not select any item', async

await expect(page.locator(SELECTORS.SIDEBAR_ITEMS.OVERVIEW)).not.toBeFocused();

});

test('Tooltip behaviour in new items', async ({ page }) => {

await visitHome(page);

await page.locator(SELECTORS.SIDEBAR_PAPER_ICON_ITEMS.GOOGLE).hover();

await expect(page.locator(SELECTORS.TOOLTIP)).not.toBeVisible();

await page.locator(SELECTORS.SIDEBAR_HA_ICON_BUTTON).click();

await expect(page.locator(SELECTORS.HA_SIDEBAR)).not.toHaveAttribute('expanded');

await page.locator(SELECTORS.SIDEBAR_PAPER_ICON_ITEMS.GOOGLE).hover();

await expect(page.locator(SELECTORS.TOOLTIP)).toBeVisible();

await expect(page.locator(SELECTORS.TOOLTIP)).toContainText('Google');

await page.locator(SELECTORS.SIDEBAR_HA_ICON_BUTTON).click();

});
1 change: 1 addition & 0 deletions tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const SELECTORS = {
HA_SIDEBAR: 'ha-sidebar',
HUI_VIEW: 'hui-view',
PAPER_LIST_BOX: 'paper-listbox',
TOOLTIP: '.tooltip',
SIDEBAR_ITEMS,
SIDEBAR_PAPER_ICON_ITEMS
};
Expand Down
Loading