-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(popup-menu): allow grouping header entries
Closes #896
- Loading branch information
1 parent
2edac2a
commit 1ba9142
Showing
8 changed files
with
380 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import classNames from 'clsx'; | ||
|
||
import { | ||
html, | ||
useMemo | ||
} from '../../ui'; | ||
|
||
/** | ||
* @typedef {import('./PopupMenuProvider').PopupMenuHeaderEntry} PopupMenuHeaderEntry | ||
*/ | ||
|
||
/** | ||
* Component that renders a popup menu header. | ||
* | ||
* @param {Object} props | ||
* @param {PopupMenuHeaderEntry[]} props.headerEntries | ||
* @param {PopupMenuHeaderEntry} props.selectedEntry | ||
* @param {(event: MouseEvent, entry: PopupMenuHeaderEntry) => void} props.onSelect | ||
* @param {(entry: PopupMenuHeaderEntry | null) => void} props.setSelectedEntry | ||
* @param {string} props.title | ||
*/ | ||
export default function PopupMenuHeader(props) { | ||
const { | ||
headerEntries, | ||
onSelect, | ||
selectedEntry, | ||
setSelectedEntry, | ||
title | ||
} = props; | ||
|
||
const groups = useMemo(() => groupEntries(headerEntries), [ headerEntries ]); | ||
|
||
return html` | ||
<div class="djs-popup-header"> | ||
<h3 class="djs-popup-title" title=${ title }>${ title }</h3> | ||
${ groups.map((group) => html` | ||
<ul key=${ group.id } class="djs-popup-header-group" data-header-group=${ group.id }> | ||
${ group.entries.map(entry => html` | ||
<li key=${ entry.id }> | ||
<${ entry.action ? 'button' : 'span' } | ||
class=${ getHeaderClasses(entry, entry === selectedEntry) } | ||
onClick=${ event => entry.action && onSelect(event, entry) } | ||
title=${ entry.title || entry.label } | ||
data-id=${ entry.id } | ||
onMouseEnter=${ () => entry.action && setSelectedEntry(entry) } | ||
onMouseLeave=${ () => entry.action && setSelectedEntry(null) } | ||
onFocus=${ () => entry.action && setSelectedEntry(entry) } | ||
onBlur=${ () => entry.action && setSelectedEntry(null) } | ||
> | ||
${(entry.imageUrl && html`<img class="djs-popup-entry-icon" src=${ entry.imageUrl } alt="" />`) || | ||
(entry.imageHtml && html`<div class="djs-popup-entry-icon" dangerouslySetInnerHTML=${ { __html: entry.imageHtml } } />`)} | ||
${ entry.label ? html` | ||
<span class="djs-popup-label">${ entry.label }</span> | ||
` : null } | ||
</${ entry.action ? 'button' : 'span' }> | ||
</li> | ||
`) } | ||
</ul> | ||
`) } | ||
</div> | ||
`; | ||
} | ||
|
||
|
||
// helpers | ||
function groupEntries(entries) { | ||
return entries.reduce((groups, entry) => { | ||
const groupId = entry.group || 'default'; | ||
|
||
const group = groups.find(group => group.id === groupId); | ||
|
||
if (group) { | ||
group.entries.push(entry); | ||
} else { | ||
groups.push({ | ||
id: groupId, | ||
entries: [ entry ] | ||
}); | ||
} | ||
|
||
return groups; | ||
}, []); | ||
} | ||
|
||
function getHeaderClasses(entry, selected) { | ||
return classNames( | ||
'entry', | ||
entry.className, | ||
entry.active ? 'active' : '', | ||
entry.disabled ? 'disabled' : '', | ||
selected ? 'selected' : '' | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.