|
| 1 | +import win98Styles from '../css/98-overrides.css?inline'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Win98MenuItem - A single item within a start menu or submenu. |
| 5 | + * Supports icons, labels, and nested submenus. |
| 6 | + */ |
| 7 | +class Win98MenuItem extends HTMLElement { |
| 8 | + constructor() { |
| 9 | + super(); |
| 10 | + this.attachShadow({ mode: 'open' }); |
| 11 | + } |
| 12 | + |
| 13 | + static get observedAttributes() { |
| 14 | + return ['label', 'icon', 'has-submenu', 'large']; |
| 15 | + } |
| 16 | + |
| 17 | + connectedCallback() { |
| 18 | + this.render(); |
| 19 | + this.setupListeners(); |
| 20 | + this.updateSubmenuState(); |
| 21 | + } |
| 22 | + |
| 23 | + attributeChangedCallback() { |
| 24 | + this.render(); |
| 25 | + this.updateSubmenuState(); |
| 26 | + } |
| 27 | + |
| 28 | + setupListeners() { |
| 29 | + this.shadowRoot.addEventListener('click', (e) => { |
| 30 | + if (!this.hasSubmenu) { |
| 31 | + this.dispatchEvent(new CustomEvent('menu-item-click', { |
| 32 | + bubbles: true, |
| 33 | + composed: true, |
| 34 | + detail: { label: this.getAttribute('label') } |
| 35 | + })); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + const submenuSlot = this.shadowRoot.querySelector('slot[name="submenu"]'); |
| 40 | + if (submenuSlot) { |
| 41 | + submenuSlot.addEventListener('slotchange', () => this.updateSubmenuState()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + updateSubmenuState() { |
| 46 | + const submenuSlot = this.shadowRoot.querySelector('slot[name="submenu"]'); |
| 47 | + const hasContent = submenuSlot && submenuSlot.assignedElements().length > 0; |
| 48 | + this.hasSubmenu = hasContent || this.hasAttribute('has-submenu'); |
| 49 | + |
| 50 | + const arrow = this.shadowRoot.querySelector('.menu-item-arrow'); |
| 51 | + if (arrow) { |
| 52 | + arrow.style.display = this.hasSubmenu ? 'block' : 'none'; |
| 53 | + } |
| 54 | + |
| 55 | + const container = this.shadowRoot.querySelector('.submenu-container'); |
| 56 | + if (container) { |
| 57 | + container.classList.toggle('has-items', this.hasSubmenu); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static get componentStyles() { |
| 62 | + return ` |
| 63 | + :host { |
| 64 | + display: block; |
| 65 | + position: relative; |
| 66 | + font-family: "Pixelated MS Sans Serif", Arial, sans-serif; |
| 67 | + font-size: 11px; |
| 68 | + } |
| 69 | +
|
| 70 | + .menu-item { |
| 71 | + display: flex; |
| 72 | + align-items: center; |
| 73 | + padding: 0 4px 0 6px; |
| 74 | + cursor: default; |
| 75 | + color: black; |
| 76 | + height: 22px; /* Win98 items are generally around 22-24px */ |
| 77 | + white-space: nowrap; |
| 78 | + position: relative; |
| 79 | + } |
| 80 | +
|
| 81 | + :host([large]) .menu-item { |
| 82 | + height: 38px; /* Taller for main menu */ |
| 83 | + padding: 0 8px; |
| 84 | + font-size: 12px; |
| 85 | + } |
| 86 | +
|
| 87 | + :host(:hover) > .menu-item { |
| 88 | + background-color: #000080; |
| 89 | + color: white; |
| 90 | + } |
| 91 | +
|
| 92 | + .menu-item-icon { |
| 93 | + width: 16px; |
| 94 | + height: 16px; |
| 95 | + margin-right: 8px; |
| 96 | + display: flex; |
| 97 | + align-items: center; |
| 98 | + justify-content: center; |
| 99 | + flex-shrink: 0; |
| 100 | + } |
| 101 | +
|
| 102 | + :host([large]) .menu-item-icon { |
| 103 | + width: 32px; |
| 104 | + height:32px; |
| 105 | + } |
| 106 | +
|
| 107 | + .menu-item-icon img, ::slotted([slot="icon"]) { |
| 108 | + max-width: 100%; |
| 109 | + max-height: 100%; |
| 110 | + image-rendering: pixelated; |
| 111 | + } |
| 112 | +
|
| 113 | + .menu-item-text { |
| 114 | + flex: 1; |
| 115 | + white-space: nowrap; |
| 116 | + padding-right: 16px; |
| 117 | + } |
| 118 | +
|
| 119 | + .menu-item-arrow { |
| 120 | + width: 0; |
| 121 | + height: 0; |
| 122 | + border-top: 4px solid transparent; |
| 123 | + border-bottom: 4px solid transparent; |
| 124 | + border-left: 4px solid black; |
| 125 | + margin-left: auto; |
| 126 | + display: none; /* Shown via JS if submenu exists */ |
| 127 | + } |
| 128 | +
|
| 129 | + :host(:hover) > .menu-item > .menu-item-arrow { |
| 130 | + border-left-color: white; |
| 131 | + } |
| 132 | +
|
| 133 | + .submenu-container { |
| 134 | + display: none; |
| 135 | + position: absolute; |
| 136 | + left: calc(100% - 1px); |
| 137 | + top: 0; |
| 138 | + background: #c0c0c0; |
| 139 | + box-shadow: inset -1px -1px #0a0a0a, inset 1px 1px #dfdfdf, inset -2px -2px grey, inset 2px 2px #fff; |
| 140 | + padding: 2px; |
| 141 | + min-width: 120px; |
| 142 | + z-index: 1000; |
| 143 | + width: max-content; /* Fit content */ |
| 144 | + } |
| 145 | +
|
| 146 | + :host([large]) .submenu-container { |
| 147 | + left: 100%; |
| 148 | + top: -2px; |
| 149 | + } |
| 150 | +
|
| 151 | + :host(:hover) > .submenu-container.has-items { |
| 152 | + display: block; |
| 153 | + } |
| 154 | + `; |
| 155 | + } |
| 156 | + |
| 157 | + render() { |
| 158 | + const label = this.getAttribute('label') || ''; |
| 159 | + const icon = this.getAttribute('icon') || ''; |
| 160 | + |
| 161 | + const win98Sheet = new CSSStyleSheet(); |
| 162 | + win98Sheet.replaceSync(win98Styles); |
| 163 | + |
| 164 | + const componentSheet = new CSSStyleSheet(); |
| 165 | + componentSheet.replaceSync(Win98MenuItem.componentStyles); |
| 166 | + |
| 167 | + this.shadowRoot.adoptedStyleSheets = [win98Sheet, componentSheet]; |
| 168 | + |
| 169 | + this.shadowRoot.innerHTML = ` |
| 170 | + <div class="menu-item" role="menuitem"> |
| 171 | + <div class="menu-item-icon"> |
| 172 | + ${icon ? `<img src="${icon}" alt="">` : ''} |
| 173 | + <slot name="icon"></slot> |
| 174 | + </div> |
| 175 | + <div class="menu-item-text"> |
| 176 | + ${label} |
| 177 | + <slot></slot> |
| 178 | + </div> |
| 179 | + <div class="menu-item-arrow"></div> |
| 180 | + </div> |
| 181 | + <div class="submenu-container" role="menu"> |
| 182 | + <slot name="submenu"></slot> |
| 183 | + </div> |
| 184 | + `; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +customElements.define('win98-menu-item', Win98MenuItem); |
| 189 | +export default Win98MenuItem; |
0 commit comments