-
Notifications
You must be signed in to change notification settings - Fork 263
Move window topbar content to menu on small screens #3872
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
Open
bodo22
wants to merge
10
commits into
main
Choose a base branch
from
2891-window-toolbar-collapse-in-very-small-windows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5261aa1
Move window topbar content to menu on small screens
d056de1
Fix reading from undefined error
58fd689
Fix WindowTopBarPluginMenu test
e65dacf
Fix WindowTopBarPluginMenu test
4d5cb2b
Merge branch 'main' into 2891-window-toolbar-collapse-in-very-small-w…
gerdesque 029bb2f
Adjust to functional components
gerdesque 64d2e7d
Merge branch 'main' into 2891-window-toolbar-collapse-in-very-small-w…
gerdesque f245ff2
Refactor menu component
gerdesque e32548f
Refactor plugin menu rendering
gerdesque 4b8979b
Adjust window top bar prop testing
gerdesque 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| import { screen, render } from '@tests/utils/test-utils'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { WindowTopBarMenu } from '../../../src/components/WindowTopBarMenu'; | ||
|
|
||
| /** create wrapper */ | ||
| function Subject({ ...props }) { | ||
| return ( | ||
| <WindowTopBarMenu | ||
| windowId="xyz" | ||
| classes={{}} | ||
| maximizeWindow={() => {}} | ||
| minimizeWindow={() => {}} | ||
| removeWindow={() => {}} | ||
| {...props} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| describe('WindowTopBarMenu', () => { | ||
| let user; | ||
| beforeEach(() => { | ||
| user = userEvent.setup(); | ||
| }); | ||
|
|
||
| it('passes correct callback to closeWindow button', async () => { | ||
| const removeWindow = vi.fn(); | ||
| render(<Subject allowClose removeWindow={removeWindow} />); | ||
| const button = screen.getByRole('button', { name: 'Close window' }); | ||
| expect(button).toBeInTheDocument(); | ||
| await user.click(button); | ||
| expect(removeWindow).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('passes correct callback to maximizeWindow button', async () => { | ||
| const maximizeWindow = vi.fn(); | ||
| render(<Subject allowMaximize maximizeWindow={maximizeWindow} />); | ||
| const button = screen.getByRole('button', { name: 'Maximize window' }); | ||
| expect(button).toBeInTheDocument(); | ||
| await user.click(button); | ||
| expect(maximizeWindow).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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,134 @@ | ||
| import React, { | ||
| useState, useRef, useEffect, useContext, | ||
| } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { styled } from '@mui/material/styles'; | ||
| import CloseIcon from '@mui/icons-material/CloseSharp'; | ||
| import classNames from 'classnames'; | ||
| import ResizeObserver from 'react-resize-observer'; | ||
| import { Portal } from '@mui/material'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import WindowTopMenuButton from '../containers/WindowTopMenuButton'; | ||
| import WindowTopBarPluginArea from '../containers/WindowTopBarPluginArea'; | ||
| import WindowTopBarPluginMenu from '../containers/WindowTopBarPluginMenu'; | ||
| import WindowTopBarTitle from '../containers/WindowTopBarTitle'; | ||
| import MiradorMenuButton from '../containers/MiradorMenuButton'; | ||
| import FullScreenButton from '../containers/FullScreenButton'; | ||
| import WindowMaxIcon from './icons/WindowMaxIcon'; | ||
| import WindowMinIcon from './icons/WindowMinIcon'; | ||
| import ns from '../config/css-ns'; | ||
| import PluginContext from '../extend/PluginContext'; | ||
|
|
||
| const IconButtonsWrapper = styled('div')({ display: 'flex' }); | ||
| const InvisibleIconButtonsWrapper = styled(IconButtonsWrapper)({ visibility: 'hidden' }); | ||
|
|
||
| /** | ||
| * removeAttributes | ||
| */ | ||
| const removeAttributes = (attributes = [], node) => { | ||
| if (!node) return; | ||
| attributes.forEach(attr => node.removeAttribute?.(attr)); | ||
| node.childNodes?.forEach(child => removeAttributes(attributes, child)); | ||
| }; | ||
|
|
||
| /** | ||
| * WindowTopBarMenu | ||
| */ | ||
| export function WindowTopBarMenu({ | ||
| removeWindow, windowId, | ||
| maximizeWindow = () => {}, maximized = false, minimizeWindow = () => {}, | ||
| allowClose, allowMaximize, allowFullscreen, allowTopMenuButton, | ||
| }) { | ||
| const { t } = useTranslation(); | ||
| const [outerW, setOuterW] = useState(); | ||
| const [visibleButtonsNum, setVisibleButtonsNum] = useState(0); | ||
| const iconButtonsWrapperRef = useRef(); | ||
| const pluginMap = useContext(PluginContext); | ||
| const portalRef = useRef(); | ||
|
|
||
| const buttons = [ | ||
| (pluginMap?.WindowTopBarPluginArea?.add?.length > 0 || pluginMap?.WindowTopBarPluginArea?.wrap?.length > 0) | ||
| && <WindowTopBarPluginArea key={`WindowTopBarPluginArea-${windowId}`} windowId={windowId} />, | ||
| allowTopMenuButton | ||
| && <WindowTopMenuButton key={`WindowTopMenuButton-${windowId}`} windowId={windowId} className={ns('window-menu-btn')} />, | ||
| allowMaximize | ||
| && ( | ||
| <MiradorMenuButton | ||
| key={`allowMaximizeMiradorMenuButton-${windowId}`} | ||
| aria-label={t(maximized ? 'minimizeWindow' : 'maximizeWindow')} | ||
| className={classNames(ns('window-maximize'), ns('window-menu-btn'))} | ||
| onClick={maximized ? minimizeWindow : maximizeWindow} | ||
| > | ||
| {maximized ? <WindowMinIcon /> : <WindowMaxIcon />} | ||
| </MiradorMenuButton> | ||
| ), | ||
| allowFullscreen | ||
| && <FullScreenButton key={`FullScreenButton-${windowId}`} className={ns('window-menu-btn')} />, | ||
| ].filter(Boolean); | ||
|
|
||
| const visibleButtons = buttons.slice(0, visibleButtonsNum); | ||
| const moreButtons = buttons.slice(visibleButtonsNum); | ||
| const moreButtonAlwaysShowing = pluginMap?.WindowTopBarPluginMenu?.add?.length > 0 | ||
| || pluginMap?.WindowTopBarPluginMenu?.wrap?.length > 0; | ||
|
|
||
| useEffect(() => { | ||
| if (!portalRef.current || outerW === undefined) return; | ||
| removeAttributes(['data-testid'], portalRef.current); | ||
| const children = Array.from(portalRef.current.childNodes || []); | ||
| let accWidth = 0; | ||
| // sum widths of top bar elements until wider than half of the available space | ||
| let newVisibleButtonsNum = children.reduce((count, child) => { | ||
| const width = child?.offsetWidth || 0; | ||
| accWidth += width; | ||
| return accWidth <= outerW * 0.5 ? count + 1 : count; | ||
| }, 0); | ||
|
|
||
| if (!moreButtonAlwaysShowing && children.length - newVisibleButtonsNum === 1) { | ||
| // when the WindowTopBarPluginMenu button is not always visible (== there are no WindowTopBarPluginMenu plugins) | ||
| // and only the first button would be hidden away on the next render | ||
| // (not changing the width, as the more button takes it's place), hide the first two buttons | ||
| newVisibleButtonsNum = Math.max(children.length - 2, 0); | ||
| } | ||
| setVisibleButtonsNum(newVisibleButtonsNum); | ||
| }, [outerW, moreButtonAlwaysShowing]); | ||
|
|
||
| return ( | ||
| <> | ||
| <Portal> | ||
| <InvisibleIconButtonsWrapper ref={portalRef}>{buttons}</InvisibleIconButtonsWrapper> | ||
| </Portal> | ||
| <ResizeObserver | ||
| // 96 to compensate for the burger menu button on the left and the close window button on the right | ||
| onResize={({ width }) => setOuterW(Math.max(width - 96, 0))} | ||
| /> | ||
| <WindowTopBarTitle windowId={windowId} /> | ||
| <IconButtonsWrapper ref={iconButtonsWrapperRef}> | ||
| {visibleButtons} | ||
| {(moreButtonAlwaysShowing || moreButtons.length > 0) && ( | ||
| <WindowTopBarPluginMenu windowId={windowId} moreButtons={moreButtons} /> | ||
| )} | ||
| {allowClose && ( | ||
| <MiradorMenuButton | ||
| aria-label={t('closeWindow')} | ||
| className={classNames(ns('window-close'), ns('window-menu-btn'))} | ||
| onClick={removeWindow} | ||
| > | ||
| <CloseIcon /> | ||
| </MiradorMenuButton> | ||
| )} | ||
| </IconButtonsWrapper> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| WindowTopBarMenu.propTypes = { | ||
| allowClose: PropTypes.bool.isRequired, | ||
| allowFullscreen: PropTypes.bool.isRequired, | ||
| allowMaximize: PropTypes.bool.isRequired, | ||
| allowTopMenuButton: PropTypes.bool.isRequired, | ||
| maximized: PropTypes.bool, | ||
| maximizeWindow: PropTypes.func, | ||
| minimizeWindow: PropTypes.func, | ||
| removeWindow: PropTypes.func.isRequired, | ||
| windowId: PropTypes.string.isRequired, | ||
| }; | ||
This file contains hidden or 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 hidden or 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.
This is a minor thing, but I'm not sure if
WindowTopBarMenuis the best name for this component, as it contains the entire content of theWindowTopBaraside from the sidebar button. Not sure what a better name would be.WindowTopBarContent?WindowTopBarControls?