-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Moved generic list styles to the
RawList
component
- Divided the list into two type, selectable and dropdown.
- Loading branch information
Showing
10 changed files
with
182 additions
and
65 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/renderer/src/components/dropdown-list/DropdownList.tsx
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,65 @@ | ||
import DropdownListItem from "./DropdownListItem"; | ||
import { useRovingFocusGroup } from "@renderer/lib/roving-focus-group/rovingFocusGroup"; | ||
import { createContext, createSignal, JSX, ParentComponent, useContext } from "solid-js"; | ||
import { RawList } from "../raw-list/RawList"; | ||
import { TokenNamespace } from "@renderer/lib/tungsten/token"; | ||
|
||
export type Props = JSX.IntrinsicElements["div"]; | ||
|
||
export type Context = ReturnType<typeof useProviderValue>; | ||
function useProviderValue() { | ||
const namespace = new TokenNamespace(); | ||
let pointerLeaveTimeout: NodeJS.Timeout | undefined; | ||
|
||
const [isHighlighted, setIsHighlighted] = createSignal(false); | ||
|
||
const rovingFocusGroup = useRovingFocusGroup({ | ||
updateFocusOnHover: true, | ||
onKeyUp: () => { | ||
setIsHighlighted(true); | ||
}, | ||
}); | ||
|
||
const handleItemPointerLeave = () => { | ||
clearTimeout(pointerLeaveTimeout); | ||
pointerLeaveTimeout = setTimeout(() => { | ||
setIsHighlighted(false); | ||
}, 60); | ||
}; | ||
|
||
const handleItemPointerMove = () => { | ||
setIsHighlighted(true); | ||
}; | ||
|
||
return { | ||
...rovingFocusGroup, | ||
handleItemPointerMove, | ||
isHighlighted, | ||
handleItemPointerLeave, | ||
namespace, | ||
}; | ||
} | ||
|
||
export const DropdownListContext = createContext<Context>(); | ||
const DropdownListRoot: ParentComponent<Props> = (props) => { | ||
const value = useProviderValue(); | ||
return ( | ||
<DropdownListContext.Provider value={value}> | ||
<RawList {...props} {...value.attrs} /> | ||
</DropdownListContext.Provider> | ||
); | ||
}; | ||
|
||
export function useDropdownList(): Context { | ||
const state = useContext(DropdownListContext); | ||
if (!state) { | ||
throw new Error("useDropdownList needs to be used inisde of the `ListContext` component."); | ||
} | ||
return state; | ||
} | ||
|
||
const DropdownList = Object.assign(DropdownListRoot, { | ||
Item: DropdownListItem, | ||
}); | ||
|
||
export default DropdownList; |
31 changes: 31 additions & 0 deletions
31
src/renderer/src/components/dropdown-list/DropdownListItem.tsx
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,31 @@ | ||
import { useDropdownList } from "./DropdownList"; | ||
import { Component, onCleanup } from "solid-js"; | ||
import { JSX } from "solid-js/jsx-runtime"; | ||
import { RawList } from "../raw-list/RawList"; | ||
|
||
export type Props = JSX.IntrinsicElements["button"]; | ||
const DropdownListItem: Component<Props> = (props) => { | ||
const state = useDropdownList(); | ||
const value = state.namespace.create(); | ||
const { attrs, tabIndex, isSelected } = state.item(value, { | ||
onPointerMove: state.handleItemPointerMove, | ||
}); | ||
|
||
onCleanup(() => { | ||
state.namespace.destroy(value); | ||
}); | ||
|
||
return ( | ||
<RawList.Item | ||
onPointerLeave={state.handleItemPointerLeave} | ||
tabIndex={tabIndex()} | ||
classList={{ | ||
"bg-overlay/30": state.isHighlighted() && isSelected(), | ||
}} | ||
{...attrs} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default DropdownListItem; |
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,24 @@ | ||
import { cn } from "@renderer/lib/css.utils"; | ||
import { Component, JSX, splitProps } from "solid-js"; | ||
|
||
export const RawListContainer: Component<JSX.IntrinsicElements["div"]> = (_props) => { | ||
const [props, rest] = splitProps(_props, ["class"]); | ||
return <div class={cn("flex flex-col gap-0.5", props.class)} {...rest} />; | ||
}; | ||
export const RawListItem: Component<JSX.IntrinsicElements["button"]> = (_props) => { | ||
const [props, rest] = splitProps(_props, ["class"]); | ||
return ( | ||
<button | ||
class={cn( | ||
"flex items-center justify-between rounded-md px-2 py-1.5 disabled:opacity-50 disabled:pointer-events-none", | ||
props.class, | ||
)} | ||
{...rest} | ||
/> | ||
); | ||
}; | ||
|
||
/** List component with only styles */ | ||
export const RawList = Object.assign(RawListContainer, { | ||
Item: RawListItem, | ||
}); |
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
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.