feat: Multiple minor changes (#769)

This commit is contained in:
Aman Harwara
2021-12-10 18:58:29 +05:30
committed by GitHub
parent 024d44f1ff
commit 24c6b831c6
2 changed files with 68 additions and 48 deletions

View File

@@ -2,11 +2,11 @@ import { WebApplication } from '@/ui_models/application';
import { CollectionSort, PrefKey } from '@standardnotes/snjs'; import { CollectionSort, PrefKey } from '@standardnotes/snjs';
import { observer } from 'mobx-react-lite'; import { observer } from 'mobx-react-lite';
import { FunctionComponent } from 'preact'; import { FunctionComponent } from 'preact';
import { useState } from 'preact/hooks'; import { useRef, useState } from 'preact/hooks';
import { Icon } from './Icon'; import { Icon } from './Icon';
import { Menu } from './menu/Menu'; import { Menu } from './menu/Menu';
import { MenuItem, MenuItemSeparator, MenuItemType } from './menu/MenuItem'; import { MenuItem, MenuItemSeparator, MenuItemType } from './menu/MenuItem';
import { toDirective } from './utils'; import { toDirective, useCloseOnClickOutside } from './utils';
type Props = { type Props = {
application: WebApplication; application: WebApplication;
@@ -108,8 +108,16 @@ flex flex-col py-2 bottom-0 left-2 absolute';
application.setPreference(PrefKey.NotesHideProtected, !hideProtected); application.setPreference(PrefKey.NotesHideProtected, !hideProtected);
}; };
const menuRef = useRef<HTMLDivElement>(null);
useCloseOnClickOutside(menuRef as any, (open: boolean) => {
if (!open) {
setShowMenuFalse();
}
});
return ( return (
<div className={menuClassName}> <div ref={menuRef} className={menuClassName}>
<Menu a11yLabel="Sort by" closeMenu={setShowMenuFalse}> <Menu a11yLabel="Sort by" closeMenu={setShowMenuFalse}>
<div className="px-3 my-1 text-xs font-semibold color-text uppercase"> <div className="px-3 my-1 text-xs font-semibold color-text uppercase">
Sort by Sort by

View File

@@ -76,7 +76,6 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
}, [focusModeEnabled]); }, [focusModeEnabled]);
const reloadThemes = useCallback(() => { const reloadThemes = useCallback(() => {
application.streamItems(ContentType.Theme, () => {
const themes = application.getDisplayableItems( const themes = application.getDisplayableItems(
ContentType.Theme ContentType.Theme
) as SNTheme[]; ) as SNTheme[];
@@ -100,31 +99,44 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
setDefaultThemeOn( setDefaultThemeOn(
!themes.find((theme) => theme.active && !theme.isLayerable()) !themes.find((theme) => theme.active && !theme.isLayerable())
); );
});
}, [application]); }, [application]);
const reloadToggleableComponents = useCallback(() => { const reloadToggleableComponents = useCallback(() => {
application.streamItems(ContentType.Component, () => {
const toggleableComponents = ( const toggleableComponents = (
application.getDisplayableItems( application.getDisplayableItems(ContentType.Component) as SNComponent[]
ContentType.Component
) as SNComponent[]
).filter((component) => ).filter((component) =>
[ComponentArea.EditorStack, ComponentArea.TagsList].includes( [ComponentArea.EditorStack, ComponentArea.TagsList].includes(
component.area component.area
) )
); );
setToggleableComponents(toggleableComponents); setToggleableComponents(toggleableComponents);
});
}, [application]); }, [application]);
useEffect(() => { useEffect(() => {
const cleanupItemStream = application.streamItems(
ContentType.Theme,
() => {
reloadThemes(); reloadThemes();
}, [reloadThemes]); }
);
return () => {
cleanupItemStream();
};
}, [application, reloadThemes]);
useEffect(() => { useEffect(() => {
const cleanupItemStream = application.streamItems(
ContentType.Component,
() => {
reloadToggleableComponents(); reloadToggleableComponents();
}, [reloadToggleableComponents]); }
);
return () => {
cleanupItemStream();
};
}, [application, reloadToggleableComponents]);
useEffect(() => { useEffect(() => {
if (themesMenuOpen) { if (themesMenuOpen) {
@@ -274,10 +286,9 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
</Disclosure> </Disclosure>
) : null} ) : null}
{toggleableComponents.map((component) => ( {toggleableComponents.map((component) => (
<Switch <button
className="sn-dropdown-item focus:bg-info-backdrop focus:shadow-none" className="sn-dropdown-item justify-between focus:bg-info-backdrop focus:shadow-none"
checked={component.active} onClick={() => {
onChange={() => {
toggleComponent(component); toggleComponent(component);
}} }}
> >
@@ -285,7 +296,8 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
<Icon type="window" className="color-neutral mr-2" /> <Icon type="window" className="color-neutral mr-2" />
{component.name} {component.name}
</div> </div>
</Switch> <Switch checked={component.active} className="px-0" />
</button>
))} ))}
<FocusModeSwitch <FocusModeSwitch
application={application} application={application}