-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ElementSelection: New element selection context to support selecting elements #97029
Conversation
…lements like panels
<Stack gap={3} alignItems={'center'}> | ||
<Checkbox | ||
label="Enable element selection" | ||
value={elementSelectionState.enabled} | ||
onChange={() => onToggleElementSelection()} | ||
/> | ||
Selected: | ||
{elementSelectionState.selected.map((item) => ( | ||
<span key={item.id}>{item.id}</span> | ||
))} | ||
</Stack> | ||
<ElementSelectionContext.Provider value={elementSelectionState}> | ||
<VizGridLayout> | ||
<PanelChrome title={'Panel title 1'} selectionId="panel-1"> | ||
Hello | ||
</PanelChrome> | ||
<PanelChrome title={'Panel title 2'} selectionId="panel-2"> | ||
Hello | ||
</PanelChrome> | ||
<PanelChrome title={'Panel title 3'} selectionId="panel-3"> | ||
Hello | ||
</PanelChrome> | ||
<PanelChrome title={'Panel title 4'} selectionId="panel-4"> | ||
Hello | ||
</PanelChrome> | ||
</VizGridLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding this demo impl, thoughts
- Remove it
- Move it to a storybook story for ElementSelectionContext
- Keep it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping it here feels weird. I'd move it to storybook or remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like the overall implementation of this. Left two comments in the code but overall I really like it.
A couple of additional comments:
- let's avoid having the timer for selection clearing. I think we can improve the existing solution in the future if it becomes a problem
- I find providers pretty awful to work with, especially because they're causing more nesting in JSX which can lead to ugly code. However, one of my comments was about having a hook that consumes the context and encapsulates the entire logic of handling selection. Or we can have both and allow devs to use whatever feels better for them?
} | ||
|
||
const isSelected = context.selected.some((item) => item.id === id); | ||
const onSelect = useCallback<React.PointerEventHandler>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is breaking the rules of hooks again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, if theres is no context and a context is added there will be an unmount/remount
so an early exit when there is no context is fine
<Stack gap={3} alignItems={'center'}> | ||
<Checkbox | ||
label="Enable element selection" | ||
value={elementSelectionState.enabled} | ||
onChange={() => onToggleElementSelection()} | ||
/> | ||
Selected: | ||
{elementSelectionState.selected.map((item) => ( | ||
<span key={item.id}>{item.id}</span> | ||
))} | ||
</Stack> | ||
<ElementSelectionContext.Provider value={elementSelectionState}> | ||
<VizGridLayout> | ||
<PanelChrome title={'Panel title 1'} selectionId="panel-1"> | ||
Hello | ||
</PanelChrome> | ||
<PanelChrome title={'Panel title 2'} selectionId="panel-2"> | ||
Hello | ||
</PanelChrome> | ||
<PanelChrome title={'Panel title 3'} selectionId="panel-3"> | ||
Hello | ||
</PanelChrome> | ||
<PanelChrome title={'Panel title 4'} selectionId="panel-4"> | ||
Hello | ||
</PanelChrome> | ||
</VizGridLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping it here feels weird. I'd move it to storybook or remove it.
@@ -14,26 +26,93 @@ export const TestStuffPage = () => { | |||
}; | |||
|
|||
const notifyApp = useAppNotification(); | |||
const [elementSelectionState, setElementSelectionState] = useState<ElementSelectionContextState>({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should make this a standalone hook that can be reused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bfmatei maybe, not sure what the real dashboard implementation will look like yet. But after we make that one we could we can see how re-usable it is
A quick proof of concept of an element selection solution.
We are exploring an edit pane in dashboards that shows actions & options for any element or elements (multiple) selected. See first POC here: #96895
In that POC, I used a very hacky global onClick handler and element attributes to mark elements as selectable and selected. However, because it relied on DOM element manipulation to add the selected state class, the selection state is visually lost whenever react re-renders. This PR is exploring a more robust react approach to element selection via a context.
Context has
Point of feedback
Outside the scope of this PR (So ignore)
element_selection_context.mp4