addons/isl/src/responsive.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {atom, useSetAtom} from 'jotai';
b69ab319import {useEffect, useRef} from 'react';
b69ab3110import {useCommand} from './ISLShortcuts';
b69ab3111import {
b69ab3112 atomWithOnChange,
b69ab3113 configBackedAtom,
b69ab3114 localStorageBackedAtom,
b69ab3115 readAtom,
b69ab3116 writeAtom,
b69ab3117} from './jotaiUtils';
b69ab3118
b69ab3119export const mainContentWidthState = atom(500);
b69ab3120
b69ab3121export const renderCompactAtom = configBackedAtom<boolean>('isl.render-compact', false);
b69ab3122
b69ab3123export const zoomUISettingAtom = atomWithOnChange(
b69ab3124 localStorageBackedAtom<number>('isl.ui-zoom', 1),
b69ab3125 newValue => {
b69ab3126 document.body?.style.setProperty('--zoom', `${newValue}`);
b69ab3127 },
b69ab3128);
b69ab3129
b69ab3130export function useZoomShortcut() {
b69ab3131 useCommand('ZoomIn', () => {
b69ab3132 const old = readAtom(zoomUISettingAtom);
b69ab3133 writeAtom(zoomUISettingAtom, Math.round((old + 0.1) * 100) / 100);
b69ab3134 });
b69ab3135 useCommand('ZoomOut', () => {
b69ab3136 const old = readAtom(zoomUISettingAtom);
b69ab3137 writeAtom(zoomUISettingAtom, Math.round((old - 0.1) * 100) / 100);
b69ab3138 });
b69ab3139}
b69ab3140
b69ab3141export function useMainContentWidth() {
b69ab3142 const setMainContentWidth = useSetAtom(mainContentWidthState);
b69ab3143
b69ab3144 const mainContentRef = useRef<null | HTMLDivElement>(null);
b69ab3145 useEffect(() => {
b69ab3146 const element = mainContentRef.current;
b69ab3147 if (element == null) {
b69ab3148 return;
b69ab3149 }
b69ab3150
b69ab3151 const obs = new ResizeObserver(entries => {
b69ab3152 const [entry] = entries;
b69ab3153 setMainContentWidth(entry.contentRect.width);
b69ab3154 });
b69ab3155 obs.observe(element);
b69ab3156 return () => obs.unobserve(element);
b69ab3157 }, [mainContentRef, setMainContentWidth]);
b69ab3158
b69ab3159 return mainContentRef;
b69ab3160}
b69ab3161
b69ab3162export const NARROW_COMMIT_TREE_WIDTH = 800;
b69ab3163export const NARROW_COMMIT_TREE_WIDTH_WHEN_COMPACT = 300;
b69ab3164
b69ab3165export const isNarrowCommitTree = atom(
b69ab3166 get =>
b69ab3167 get(mainContentWidthState) <
b69ab3168 (get(renderCompactAtom) ? NARROW_COMMIT_TREE_WIDTH_WHEN_COMPACT : NARROW_COMMIT_TREE_WIDTH),
b69ab3169);