Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Search and Tree in sidebar and implement proper keyboard navi…
…gation.
- Loading branch information
1 parent
aa29c91
commit b109fef
Showing
34 changed files
with
29,197 additions
and
1,011 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
|
||
import Explorer from './Explorer'; | ||
import { mockDataset } from './mockdata'; | ||
import { RefType } from './RefHelpers'; | ||
|
||
export default { | ||
component: Explorer, | ||
title: 'UI/Sidebar/Explorer', | ||
excludeStories: /.*Data$/, | ||
}; | ||
|
||
const selected = { | ||
refId: 'storybook_internal', | ||
storyId: '1-12-121', | ||
}; | ||
|
||
const simple: Record<string, RefType> = { | ||
storybook_internal: { | ||
title: null, | ||
id: 'storybook_internal', | ||
url: 'iframe.html', | ||
ready: true, | ||
stories: mockDataset.withRoot, | ||
}, | ||
}; | ||
|
||
const withRefs: Record<string, RefType> = { | ||
...simple, | ||
basic: { | ||
id: 'basic', | ||
title: 'Basic ref', | ||
url: 'https://example.com', | ||
ready: true, | ||
type: 'auto-inject', | ||
stories: mockDataset.noRoot, | ||
}, | ||
injected: { | ||
id: 'injected', | ||
title: 'Not ready', | ||
url: 'https://example.com', | ||
ready: false, | ||
type: 'auto-inject', | ||
stories: mockDataset.noRoot, | ||
}, | ||
unknown: { | ||
id: 'unknown', | ||
title: 'Unknown ref', | ||
url: 'https://example.com', | ||
ready: true, | ||
type: 'unknown', | ||
stories: mockDataset.noRoot, | ||
}, | ||
lazy: { | ||
id: 'lazy', | ||
title: 'Lazy loaded ref', | ||
url: 'https://example.com', | ||
ready: false, | ||
type: 'lazy', | ||
stories: mockDataset.withRoot, | ||
}, | ||
}; | ||
|
||
export const Simple = () => ( | ||
<Explorer dataset={{ hash: simple, entries: Object.entries(simple) }} selected={selected} /> | ||
); | ||
|
||
export const WithRefs = () => ( | ||
<Explorer dataset={{ hash: withRefs, entries: Object.entries(withRefs) }} 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* eslint-env browser */ | ||
|
||
import React, { FunctionComponent, useEffect, useRef, useState, useCallback } from 'react'; | ||
|
||
import throttle from 'lodash/throttle'; | ||
|
||
import { Ref } from './Refs'; | ||
import { CombinedDataset, Selection } from './types'; | ||
|
||
function cycleArray<T>(array: T[], index: number, delta: number): T { | ||
let next = index + (delta % array.length); | ||
if (next < 0) next = array.length + next; | ||
if (next >= array.length) next -= array.length; | ||
return array[next]; | ||
} | ||
|
||
const scrollIntoView = ( | ||
element: Element, | ||
options: ScrollIntoViewOptions = { block: 'nearest' } | ||
) => { | ||
if (!element) return; | ||
setTimeout(() => { | ||
const { top, bottom } = element.getBoundingClientRect(); | ||
const isInView = | ||
top >= 0 && bottom <= (window.innerHeight || document.documentElement.clientHeight); | ||
if (!isInView) element.scrollIntoView(options); | ||
}, 0); | ||
}; | ||
|
||
interface ExplorerProps { | ||
dataset: CombinedDataset; | ||
selected: Selection; | ||
} | ||
|
||
const Explorer: FunctionComponent<ExplorerProps> = React.memo(({ dataset, selected }) => { | ||
const rootRef = useRef<HTMLDivElement>(null); | ||
const highlightedRef = useRef<Selection>(selected); | ||
|
||
const [highlighted, setHighlighted] = useState<Selection>(selected); | ||
const highlightElement = useCallback( | ||
(element: Element) => { | ||
const selection = { | ||
storyId: element.getAttribute('data-id'), | ||
refId: element.getAttribute('data-ref'), | ||
}; | ||
scrollIntoView(element); | ||
setHighlighted(selection); | ||
highlightedRef.current = selection; | ||
}, | ||
[setHighlighted] | ||
); | ||
|
||
useEffect(() => { | ||
const { storyId, refId } = selected; | ||
const element = rootRef.current.querySelector(`[data-id="${storyId}"][data-ref="${refId}"]`); | ||
scrollIntoView(element, { block: 'center' }); | ||
setHighlighted(selected); | ||
highlightedRef.current = selected; | ||
}, [dataset, highlightedRef, selected]); // dataset is needed here | ||
|
||
useEffect(() => { | ||
const navigateTree = throttle((event) => { | ||
if (!event.key || !rootRef || !rootRef.current) return; | ||
if (event.shiftKey || event.metaKey || event.ctrlKey || event.altKey) return; | ||
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') { | ||
event.preventDefault(); | ||
const focusable = Array.from(rootRef.current.querySelectorAll('[data-highlightable=true]')); | ||
const focusedIndex = focusable.findIndex( | ||
(el) => | ||
el.getAttribute('data-id') === highlightedRef.current.storyId && | ||
el.getAttribute('data-ref') === highlightedRef.current.refId | ||
); | ||
highlightElement(cycleArray(focusable, focusedIndex, event.key === 'ArrowUp' ? -1 : 1)); | ||
} | ||
}, 16); | ||
|
||
document.addEventListener('keydown', navigateTree); | ||
return () => document.removeEventListener('keydown', navigateTree); | ||
}, [highlightedRef, highlightElement]); | ||
|
||
return ( | ||
<div ref={rootRef}> | ||
{dataset.entries.map(([refId, ref]) => ( | ||
<Ref | ||
{...ref} | ||
key={refId} | ||
selectedId={selected.refId === ref.id ? selected.storyId : null} | ||
highlightedId={highlighted.refId === ref.id ? highlighted.storyId : null} | ||
setHighlighted={setHighlighted} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}); | ||
|
||
export default Explorer; |
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.