UNPKG

@dfinity/gix-components

Version:
28 lines (27 loc) 894 B
import { Menu } from "../types/menu"; import { isNode } from "./env.utils"; import { enumFromStringExists } from "./enum.utils"; export const MENU_ATTRIBUTE = "menu"; export const LOCALSTORAGE_MENU_KEY = "nnsMenu"; export const initMenu = () => { // No DOM therefore cannot guess the menu if (isNode()) { return undefined; } const menu = document.documentElement.getAttribute(MENU_ATTRIBUTE); const initialMenu = enumFromStringExists({ obj: Menu, value: menu, }) ? menu : Menu.EXPANDED; applyMenu({ menu: initialMenu, preserve: false }); return initialMenu; }; export const applyMenu = ({ menu, preserve = true, }) => { const { documentElement } = document; documentElement.setAttribute(MENU_ATTRIBUTE, menu); if (preserve) { localStorage.setItem(LOCALSTORAGE_MENU_KEY, JSON.stringify(menu)); } };