-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat(ActionBar): logic refactor #1219
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e9ded0
feat(ActionBar): logic refactor
marcinsawicki e5b581b
feat(ActionBar): story change
marcinsawicki bd9f7e9
feat(ActionBar): change after review
marcinsawicki 169bc4d
feat(ActionBar): fix for logic loop
marcinsawicki 18f39ac
feat(ActionBar): changes after review
marcinsawicki 6d8bd76
Merge branch 'main' into feature/action-bar-menu-icon-fix
VadymBezpalko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ import { Button } from '../Button'; | |
import { Icon } from '../Icon'; | ||
|
||
import { ActionBarItem } from './ActionBarItem'; | ||
import { IActionBarProps } from './types'; | ||
import { IActionBarOption, IActionBarProps } from './types'; | ||
|
||
import styles from './ActionBar.module.scss'; | ||
|
||
|
@@ -24,89 +24,57 @@ export const ActionBar: React.FC<IActionBarProps> = ({ | |
vertical, | ||
menuFooter, | ||
}) => { | ||
const [menuItemsKeys, setMenuItemsKeys] = React.useState<string[]>([]); | ||
const [menuPosition, setMenuPosition] = React.useState<number>(0); | ||
const [visibleItemsCount, setVisibleItemsCount] = React.useState<number>( | ||
options.length | ||
); | ||
const [menuOptions, setMenuOptions] = React.useState<IActionBarOption[]>([]); | ||
const [isMenuOpen, setIsMenuOpen] = React.useState<boolean>(false); | ||
const isScrollType = type === 'scroll'; | ||
const mergedClassNames = cx( | ||
styles[baseClass], | ||
className, | ||
vertical && styles[`${baseClass}--vertical`] | ||
); | ||
const observerOptions = { | ||
root: document.querySelector(`${id}`), | ||
threshold: 1, | ||
}; | ||
const shouldDisplayMenu = !isScrollType && menuItemsKeys.length !== 0; | ||
const singleElementSize = 44; | ||
|
||
React.useEffect(() => { | ||
if (isScrollType) { | ||
return; | ||
} | ||
|
||
// Single element size with margin | ||
const singleElementSize = 44; | ||
// Extra spacing to include for menu placement | ||
const menuPlacementSpacing = 4; | ||
const allOptionsCount = options.length; | ||
const hiddenOptionsCount = menuItemsKeys.length; | ||
const visibleOptionsCount = allOptionsCount - hiddenOptionsCount; | ||
const position = | ||
visibleOptionsCount * singleElementSize + menuPlacementSpacing; | ||
|
||
setMenuPosition(position); | ||
}, [menuItemsKeys, options, isScrollType]); | ||
|
||
const handleIntersection = (entries: IntersectionObserverEntry[]) => { | ||
entries.map((entry) => { | ||
const entryExistInMenu = menuItemsKeys.includes(entry.target.id); | ||
|
||
if (!entry.isIntersecting) { | ||
entry.target.setAttribute('tabindex', '-1'); | ||
|
||
if (!entryExistInMenu) { | ||
setMenuItemsKeys((prevItemKeys) => [ | ||
...prevItemKeys, | ||
entry.target.id, | ||
]); | ||
} | ||
|
||
return; | ||
} | ||
|
||
entry.target.removeAttribute('tabindex'); | ||
|
||
if (entryExistInMenu) { | ||
setMenuItemsKeys(menuItemsKeys.filter((i) => i !== entry.target.id)); | ||
} | ||
}); | ||
}; | ||
if (options.length !== visibleItemsCount) { | ||
setMenuOptions(options.slice(visibleItemsCount, options.length)); | ||
} else { | ||
setMenuOptions([]); | ||
} | ||
}, [options, visibleItemsCount]); | ||
|
||
const shouldDisplayMenu = !isScrollType && menuOptions.length !== 0; | ||
|
||
React.useEffect(() => { | ||
const hasIOSupport = !!window.IntersectionObserver; | ||
const hasIOSupport = !!window.ResizeObserver; | ||
|
||
if (!isScrollType && hasIOSupport) { | ||
const target = document.querySelectorAll( | ||
`button[data-actionbarid='${id}']` | ||
); | ||
const observer = new ResizeObserver((entries) => { | ||
const { width, height } = entries[0].contentRect; | ||
const containerSize = vertical ? height : width; | ||
const exstraSpacing = menuOptions.length > 0 ? 60 : 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const observer = new IntersectionObserver( | ||
handleIntersection, | ||
observerOptions | ||
); | ||
const visibleOptionsCount = Math.floor( | ||
(containerSize - exstraSpacing) / singleElementSize | ||
); | ||
setVisibleItemsCount(visibleOptionsCount); | ||
}); | ||
|
||
target.forEach((e) => observer.observe(e)); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
observer.observe(document.querySelector(`#${id}`)!); | ||
|
||
return () => observer.disconnect(); | ||
} | ||
}, [menuItemsKeys, isScrollType]); | ||
|
||
const getMenuItems = (keys: string[]) => { | ||
const filteredOptions = options.filter((row) => | ||
keys.find((k) => k === row.key && !row.hideInMenu) | ||
); | ||
}, [menuOptions, isScrollType]); | ||
|
||
return filteredOptions.map((o) => { | ||
const getMenuItems = (menuOptions: IActionBarOption[]) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useCallback? |
||
menuOptions.map((o) => { | ||
return { | ||
key: o.key, | ||
element: ( | ||
|
@@ -115,37 +83,19 @@ export const ActionBar: React.FC<IActionBarProps> = ({ | |
onClick: o.onClick, | ||
}; | ||
}); | ||
}; | ||
|
||
const buttonElement = options | ||
.filter((row) => menuItemsKeys.find((i) => i === row.key)) | ||
.find((o) => o.key === activeOptionKey); | ||
|
||
const getMenuPosition = (position: number, vertical?: boolean) => { | ||
if (vertical) { | ||
return { | ||
top: position, | ||
}; | ||
} | ||
|
||
return { | ||
left: position, | ||
}; | ||
}; | ||
const buttonElement = menuOptions.find((o) => o.key === activeOptionKey); | ||
|
||
return ( | ||
<div id={id} className={mergedClassNames}> | ||
<div | ||
className={cx(styles[`${baseClass}__items`], { | ||
[styles[`${baseClass}__items--scroll`]]: isScrollType, | ||
[styles[`${baseClass}__items--with-menu`]]: shouldDisplayMenu, | ||
})} | ||
> | ||
{options.map((o) => ( | ||
{options.slice(0, visibleItemsCount).map((o) => ( | ||
<ActionBarItem | ||
id={id} | ||
option={o} | ||
isHidden={menuItemsKeys.includes(o.key)} | ||
isActive={o.key === activeOptionKey} | ||
vertical={vertical} | ||
/> | ||
|
@@ -158,15 +108,14 @@ export const ActionBar: React.FC<IActionBarProps> = ({ | |
buttonElement && styles[`${menuWrapperClass}--active`], | ||
vertical && styles[`${menuWrapperClass}--vertical`] | ||
)} | ||
style={getMenuPosition(menuPosition, vertical)} | ||
> | ||
<ActionMenu | ||
selectedOptions={activeOptionKey ? [activeOptionKey] : []} | ||
onOpen={() => setIsMenuOpen(true)} | ||
onClose={() => setIsMenuOpen(false)} | ||
floatingStrategy="fixed" | ||
placement={vertical ? 'left-start' : 'bottom-end'} | ||
options={getMenuItems(menuItemsKeys)} | ||
options={getMenuItems(menuOptions)} | ||
triggerClassName={cx( | ||
vertical && styles[`${menuWrapperClass}__trigger-vertical`] | ||
)} | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't it be available as a prop? If not I'd suggest to move it outside the component's function.
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.
Currently not, but in the future I plan to improve that component and give a possibility to set the buttons size.