-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d99107
commit 7bab1e8
Showing
13 changed files
with
317 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useState } from 'react'; | ||
|
||
export interface ContextMenuProps { | ||
children: React.ReactChild[] | React.ReactChild; | ||
menuEntries: ContextMenuEntry[]; | ||
} | ||
|
||
export interface ContextMenuEntry { | ||
title: string; | ||
active?: boolean; | ||
/** | ||
* whether a separator line should be rendered below this item | ||
*/ | ||
separator?: boolean; | ||
customFontStyle?: string; | ||
onClick?: ()=>void; | ||
href?: string; | ||
} | ||
|
||
function ContextMenu(props: ContextMenuProps) { | ||
const [expanded, setExpanded] = useState(false); | ||
const toggleExpanded = () => { | ||
setExpanded(!expanded); | ||
} | ||
|
||
if (expanded) { | ||
// HACK! I want to skip the bubbling phase of the current click | ||
setTimeout(() => { | ||
window.addEventListener('click', () => setExpanded(false), { once: true }); | ||
}, 0); | ||
} | ||
|
||
const enhancedEntries = props.menuEntries.map(e => { | ||
return { | ||
... e, | ||
onClick: () => { | ||
e.onClick && e.onClick(); | ||
toggleExpanded(); | ||
} | ||
} | ||
}) | ||
const font = "text-gray-400 hover:text-gray-800" | ||
return ( | ||
<div className="relative cursor-pointer"> | ||
<div onClick={(e) => { | ||
toggleExpanded(); | ||
e.preventDefault(); | ||
}}> | ||
{props.children} | ||
</div> | ||
{expanded? | ||
<div className={`z-50 w-40 bg-white absolute py-2 right-0 flex flex-col border border-gray-200 rounded-lg space-y-2`}> | ||
{enhancedEntries.map(e => { | ||
const entry = <div key={e.title} className={`px-4 flex py-2 text-gray-600 hover:bg-gray-200 text-sm leading-1 ${e.customFontStyle || font} ${e.separator? ' border-b border-gray-200':''}`} > | ||
<div>{e.title}</div><div className="flex-1"></div>{e.active ? <div className="pl-1 font-semibold">✓</div>: null} | ||
</div> | ||
return <a href={e.href} onClick={e.onClick}> | ||
{entry} | ||
</a> | ||
})} | ||
</div> | ||
: | ||
null | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
export default ContextMenu; |
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,35 @@ | ||
import { useState } from 'react'; | ||
import ContextMenu from './ContextMenu'; | ||
|
||
export interface DropDownProps { | ||
entries: { | ||
title: string, | ||
onClick: ()=>void | ||
}[]; | ||
} | ||
|
||
function Arrow(props: {up: boolean}) { | ||
return <span className="mx-2 border-gray-400" style={{ margin: 2, padding: 3, border: 'solid black', borderWidth: '0 2px 2px 0', display: 'inline-block', transform: `rotate(${props.up ? '-135deg' : '45deg'})`}}></span> | ||
} | ||
|
||
function DropDown(props: DropDownProps) { | ||
const [current, setCurrent] = useState(props.entries[0].title); | ||
const enhancedEntries = props.entries.map(e => { | ||
return { | ||
...e, | ||
active: e.title === current, | ||
onClick: () => { | ||
e.onClick(); | ||
setCurrent(e.title); | ||
} | ||
} | ||
}) | ||
const font = "text-gray-400 text-sm leading-1" | ||
return ( | ||
<ContextMenu menuEntries={enhancedEntries}> | ||
<span className={`py-2 cursor-pointer ${font}`}>{current}<Arrow up={false}/></span> | ||
</ContextMenu> | ||
); | ||
} | ||
|
||
export default DropDown; |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function Separator() { | ||
return <div className="bg-gray-200 h-0.5 absolute left-0 w-screen"></div>; | ||
return <div className="border-gray-200 border-b h-0.5 absolute left-0 w-screen"></div>; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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.