Skip to content
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: add auto focus as a setting #848

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions apps/antalmanac/src/components/Header/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Box, Button, ButtonGroup, Divider, Drawer, IconButton, Typography, useM
import { CSSProperties } from '@material-ui/core/styles/withStyles';
import { Close, DarkMode, LightMode, Settings, SettingsBrightness } from '@mui/icons-material';

import { useThemeStore, useTimeFormatStore } from '$stores/SettingsStore';
import { useAutofocusStore, useThemeStore, useTimeFormatStore } from '$stores/SettingsStore';

const lightSelectedStyle: CSSProperties = {
backgroundColor: '#F0F7FF',
Expand Down Expand Up @@ -92,7 +92,7 @@ function TimeMenu() {

return (
<Box sx={{ padding: '1rem 1rem 0 1rem', width: '100%' }}>
<Typography variant="h6" style={{ marginTop: '1.5rem', marginBottom: '1rem' }}>
<Typography variant="h6" style={{ marginBottom: '1rem' }}>
Time
</Typography>

Expand Down Expand Up @@ -135,6 +135,59 @@ function TimeMenu() {
);
}

function AutofocusMenu() {
const [autoFocus, setAutofocus] = useAutofocusStore((store) => [store.autofocus, store.setAutofocus]);
const theme = useThemeStore((store) => store.appTheme);

const handleAutofocusChange = (event: React.MouseEvent<HTMLButtonElement>) => {
setAutofocus(event.currentTarget.value == 'true');
};

return (
<Box sx={{ padding: '1rem 1rem 0 1rem', width: '100%' }}>
<Typography variant="h6" style={{ marginBottom: '1rem' }}>
Autofocus Search
</Typography>

<ButtonGroup
style={{
display: 'flex',
placeContent: 'center',
width: '100%',
}}
>
<Button
style={{
padding: '1rem 2rem',
borderRadius: '12px 0px 0px 12px',
width: '100%',
fontSize: '12px',
...getSelectedStyle('false', autoFocus.toString(), theme),
}}
value="false"
onClick={handleAutofocusChange}
fullWidth={true}
>
Disabled
</Button>
<Button
style={{
padding: '1rem 2rem',
borderRadius: '0px 12px 12px 0px',
width: '100%',
fontSize: '12px',
...getSelectedStyle('true', autoFocus.toString(), theme),
}}
value="true"
onClick={handleAutofocusChange}
>
Enabled
</Button>
</ButtonGroup>
</Box>
);
}

function SettingsMenu() {
const [drawerOpen, setDrawerOpen] = useState(false);
const isMobileScreen = useMediaQuery('(max-width:750px)');
Expand Down Expand Up @@ -178,6 +231,7 @@ function SettingsMenu() {

<ThemeMenu />
<TimeMenu />
<AutofocusMenu />
</Box>
</Drawer>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type SearchResult = ReturnType<typeof search>;

import RightPaneStore from '../../RightPaneStore';
import analyticsEnum, { logAnalytics } from '$lib/analytics';
import { useAutofocusStore } from '$stores/SettingsStore';

const emojiMap: Record<string, string> = {
GE_CATEGORY: '🏫', // U+1F3EB :school:
Expand Down Expand Up @@ -182,7 +183,7 @@ class FuzzySearch extends PureComponent<FuzzySearchProps, FuzzySearchState> {
<TextField
{...params}
inputRef={(input: HTMLInputElement | null) => {
if (input && !isMobile()) {
if (input && !isMobile() && useAutofocusStore.getState().autofocus) {
input.focus();
}
}}
Expand Down
27 changes: 23 additions & 4 deletions apps/antalmanac/src/stores/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const useThemeStore = create<ThemeStore>((set) => {
themeSetting !== 'system'
? themeSetting
: window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
? 'dark'
: 'light';

return {
themeSetting: themeSetting as 'light' | 'dark' | 'system',
Expand All @@ -35,8 +35,8 @@ export const useThemeStore = create<ThemeStore>((set) => {
themeSetting !== 'system'
? themeSetting
: window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
? 'dark'
: 'light';

set({ appTheme: appTheme, themeSetting: themeSetting });

Expand Down Expand Up @@ -67,3 +67,22 @@ export const useTimeFormatStore = create<TimeFormatStore>((set) => {
},
};
});

export interface AutofocusStore {
autofocus: boolean;
setAutofocus: (autofocus: boolean) => void;
}

export const useAutofocusStore = create<AutofocusStore>((set) => {
const autofocus = typeof Storage !== 'undefined' && window.localStorage.getItem('autofocus') == 'true';

return {
autofocus,
setAutofocus: (autofocus) => {
if (typeof Storage !== 'undefined') {
window.localStorage.setItem('autofocus', autofocus.toString());
}
set({ autofocus });
},
};
});
Loading