-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: modal and search component fixes
- Loading branch information
Showing
8 changed files
with
156 additions
and
145 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
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
37 changes: 22 additions & 15 deletions
37
components/header-bar/src/command-palette/hooks/use-view-handler.js
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,25 +1,32 @@ | ||
import { useCallback } from 'react' | ||
import { useCommandPaletteContext } from '../context/command-palette-context.js' | ||
import { HOME_VIEW } from '../utils/constants.js' | ||
import { ACTIONS_SECTION, GRID_SECTION, HOME_VIEW } from '../utils/constants.js' | ||
|
||
const useViewHandler = ({ section }) => { | ||
const { setHighlightedIndex, setFilter, setCurrentView, setActiveSection } = | ||
useCommandPaletteContext() | ||
const useViewAndSectionHandler = () => { | ||
const { | ||
setHighlightedIndex, | ||
setFilter, | ||
setCurrentView, | ||
setActiveSection, | ||
showGrid, | ||
} = useCommandPaletteContext() | ||
|
||
const goToDefaultSection = useCallback(() => { | ||
const defaultSection = showGrid ? GRID_SECTION : ACTIONS_SECTION | ||
setActiveSection(defaultSection) | ||
setHighlightedIndex(0) | ||
}, [showGrid, setActiveSection, setHighlightedIndex]) | ||
|
||
const goToDefaultView = useCallback(() => { | ||
setFilter('') | ||
setCurrentView(HOME_VIEW) | ||
setActiveSection(section) | ||
setHighlightedIndex(0) | ||
}, [ | ||
setActiveSection, | ||
setCurrentView, | ||
setFilter, | ||
setHighlightedIndex, | ||
section, | ||
]) | ||
goToDefaultSection() | ||
}, [setCurrentView, setFilter, goToDefaultSection]) | ||
|
||
return goToDefaultView | ||
return { | ||
goToDefaultView, | ||
goToDefaultSection, | ||
} | ||
} | ||
|
||
export default useViewHandler | ||
export default useViewAndSectionHandler |
40 changes: 0 additions & 40 deletions
40
components/header-bar/src/command-palette/sections/container.js
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
components/header-bar/src/command-palette/sections/modal-container.js
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 { colors, elevations } from '@dhis2/ui-constants' | ||
import PropTypes from 'prop-types' | ||
import React, { forwardRef, useEffect } from 'react' | ||
|
||
const ModalContainer = forwardRef(function ModalContainer( | ||
{ children, onKeyDown }, | ||
ref | ||
) { | ||
useEffect(() => { | ||
const activeItem = ref?.current?.querySelector('.highlighted') | ||
if (activeItem && typeof activeItem.scrollIntoView === 'function') { | ||
activeItem?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }) | ||
} | ||
}, [ref]) | ||
|
||
useEffect(() => { | ||
if (!ref.current) { | ||
return | ||
} | ||
const dialog = ref.current | ||
|
||
const handleFocus = (event) => { | ||
if (event.target === ref?.current) { | ||
ref.current?.querySelector('input').focus() | ||
} | ||
} | ||
|
||
dialog.addEventListener('focus', handleFocus) | ||
dialog.addEventListener('keydown', onKeyDown) | ||
|
||
return () => { | ||
dialog.removeEventListener('focus', handleFocus) | ||
dialog.removeEventListener('keydown', onKeyDown) | ||
} | ||
}, [onKeyDown, ref]) | ||
|
||
return ( | ||
<> | ||
<dialog ref={ref}>{children}</dialog> | ||
<style jsx>{` | ||
dialog { | ||
display: flex; | ||
flex-direction: row; | ||
border: none; | ||
border-radius: 1px; | ||
padding: 1px; | ||
width: 572px; | ||
max-height: 544px; | ||
margin: 0 auto; | ||
border-radius: 3px; | ||
background: ${colors.white}; | ||
box-shadow: ${elevations.e100}; | ||
margin-top: 92px; | ||
} | ||
`}</style> | ||
</> | ||
) | ||
}) | ||
|
||
ModalContainer.propTypes = { | ||
children: PropTypes.node, | ||
onKeyDown: PropTypes.func, | ||
} | ||
|
||
export default ModalContainer |
Oops, something went wrong.