Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
fix(sn-filter-pane): reset zoom for all scenarios (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanlahti authored Dec 5, 2023
1 parent 28e26a3 commit fe3ca8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import KEYS from '../keys';
import useFocusListener from '../../hooks/use-focus-listener';
import findNextIndex from './find-next-index';
import { IEnv } from '../../types/types';
import resetZoom from '../../utils/reset-zoom';

function ListboxGrid({ stores }: { stores: IStores }) {
const { store, resourceStore } = stores;
Expand Down Expand Up @@ -89,6 +90,8 @@ function ListboxGrid({ stores }: { stores: IStores }) {
preventDefaultBehavior(event);
};

const handleFocus = sense?.isSmallDevice?.() ? () => resetZoom() : undefined;

useFocusListener(gridRef, keyboard);

const isRtl = options.direction === 'rtl';
Expand Down Expand Up @@ -116,6 +119,7 @@ function ListboxGrid({ stores }: { stores: IStores }) {
className="filter-pane-container"
container
onKeyDown={handleKeyDown}
onFocus={handleFocus}
sx={{ flexDirection: isRtl ? 'row-reverse' : 'row' }}
columns={columns?.length}
ref={gridRef as unknown as () => HTMLObjectElement}
Expand Down
11 changes: 0 additions & 11 deletions packages/sn-filter-pane/src/components/ListboxPopoverContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ const StyledPopover = styled(Popover, { shouldForwardProp: (p: string) => !['isS
} : {},
}));

function resetZoom() {
const viewportMetaTag = document.querySelector('meta[name="viewport"]');
if (viewportMetaTag instanceof HTMLMetaElement) {
viewportMetaTag.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
}
}

/**
* If a single resource is passed to this component a FoldedListbox is rendered.
* When the FoldedListbox is clicked, a Listbox is rendered in a Popover.
Expand All @@ -87,10 +80,6 @@ export const ListboxPopoverContainer = ({ resources, stores }: FoldedPopoverProp
const handleOpen = ({ event }: FoldedListboxClickEvent | { event: React.MouseEvent<HTMLButtonElement, MouseEvent> }) => {
if (event.currentTarget.contains(event.target as Node) && !constraints?.active) {
setPopoverOpen(true);
if (isSmallDevice) {
// Autofocus can cause the browser to zoom so we need to reset zoom.
resetZoom();
}
}
};
const handleClose = () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/sn-filter-pane/src/utils/reset-zoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Run this on small devices to reset the zoom. Required when focusing
* an input field and the browser auto zooms the page. Browsers do not
* expose any API for handling this currently.
*/
export default function resetZoom() {
const viewportMetaTag = document.querySelector('meta[name="viewport"]');
if (viewportMetaTag instanceof HTMLMetaElement) {
viewportMetaTag.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
}
}

0 comments on commit fe3ca8b

Please sign in to comment.