Skip to content

Commit

Permalink
fix(sn-filter-pane): reset zoom for all scenarios (#392) (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanlahti committed Jan 17, 2024
1 parent a4e20bb commit a72abe9
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 @@ -24,6 +24,7 @@ import { RenderTrackerService } from '../../services/render-tracker';
import useFocusListener from '../../hooks/use-focus-listener';
import findNextIndex from './find-next-index';
import { IEnv } from '../../types/types';
import resetZoom from '../../utils/reset-zoom';

const prepareRenderTracker = (listboxCount: number, renderTracker?: RenderTrackerService) => {
renderTracker?.setNumberOfListboxes(listboxCount);
Expand Down Expand Up @@ -117,6 +118,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 @@ -146,6 +149,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 () => HTMLDivElement}
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 @@ -68,13 +68,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 @@ -95,10 +88,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 a72abe9

Please sign in to comment.