Improve usability of icon buttons in header with tooltip
This commit is contained in:
parent
9c957952fb
commit
f860fd2cfe
4 changed files with 66 additions and 53 deletions
|
@ -1,4 +1,5 @@
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { useState } from 'react';
|
||||||
import { Link, NavLink } from 'react-router-dom';
|
import { Link, NavLink } from 'react-router-dom';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import { faCogs, faLayerGroup, faSignOutAlt, faUserCircle } from '@fortawesome/free-solid-svg-icons';
|
import { faCogs, faLayerGroup, faSignOutAlt, faUserCircle } from '@fortawesome/free-solid-svg-icons';
|
||||||
|
@ -9,27 +10,9 @@ import tw, { theme } from 'twin.macro';
|
||||||
import styled from 'styled-components/macro';
|
import styled from 'styled-components/macro';
|
||||||
import http from '@/api/http';
|
import http from '@/api/http';
|
||||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
import { useState } from 'react';
|
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
||||||
|
|
||||||
const Navigation = styled.div`
|
|
||||||
${tw`w-full bg-neutral-900 shadow-md overflow-x-auto`};
|
|
||||||
|
|
||||||
& > div {
|
|
||||||
${tw`mx-auto w-full flex items-center`};
|
|
||||||
}
|
|
||||||
|
|
||||||
& #logo {
|
|
||||||
${tw`flex-1`};
|
|
||||||
|
|
||||||
& > a {
|
|
||||||
${tw`text-2xl font-header px-4 no-underline text-neutral-200 hover:text-neutral-100 transition-colors duration-150`};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const RightNavigation = styled.div`
|
const RightNavigation = styled.div`
|
||||||
${tw`flex h-full items-center justify-center`};
|
|
||||||
|
|
||||||
& > a, & > button, & > .navigation-link {
|
& > a, & > button, & > .navigation-link {
|
||||||
${tw`flex items-center h-full no-underline text-neutral-300 px-6 cursor-pointer transition-all duration-150`};
|
${tw`flex items-center h-full no-underline text-neutral-300 px-6 cursor-pointer transition-all duration-150`};
|
||||||
|
|
||||||
|
@ -38,7 +21,7 @@ const RightNavigation = styled.div`
|
||||||
}
|
}
|
||||||
|
|
||||||
&:active, &:hover, &.active {
|
&:active, &:hover, &.active {
|
||||||
box-shadow: inset 0 -2px ${theme`colors.cyan.700`.toString()};
|
box-shadow: inset 0 -2px ${theme`colors.cyan.600`.toString()};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -57,32 +40,43 @@ export default () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Navigation>
|
<div className={'w-full bg-neutral-900 shadow-md overflow-x-auto'}>
|
||||||
<SpinnerOverlay visible={isLoggingOut} />
|
<SpinnerOverlay visible={isLoggingOut}/>
|
||||||
<div css={tw`mx-auto w-full flex items-center`} style={{ maxWidth: '1200px', height: '3.5rem' }}>
|
<div className={'mx-auto w-full flex items-center h-[3.5rem] max-w-[1200px]'}>
|
||||||
<div id={'logo'}>
|
<div id={'logo'} className={'flex-1'}>
|
||||||
<Link to={'/'}>
|
<Link
|
||||||
|
to={'/'}
|
||||||
|
className={'text-2xl font-header px-4 no-underline text-neutral-200 hover:text-neutral-100 transition-colors duration-150'}
|
||||||
|
>
|
||||||
{name}
|
{name}
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
<RightNavigation>
|
<RightNavigation className={'flex h-full items-center justify-center'}>
|
||||||
<SearchContainer/>
|
<SearchContainer/>
|
||||||
<NavLink to={'/'} exact>
|
<Tooltip placement={'bottom'} content={'Dashboard'}>
|
||||||
<FontAwesomeIcon icon={faLayerGroup}/>
|
<NavLink to={'/'} exact>
|
||||||
</NavLink>
|
<FontAwesomeIcon icon={faLayerGroup}/>
|
||||||
<NavLink to={'/account'}>
|
</NavLink>
|
||||||
<FontAwesomeIcon icon={faUserCircle}/>
|
</Tooltip>
|
||||||
</NavLink>
|
<Tooltip placement={'bottom'} content={'Account Settings'}>
|
||||||
|
<NavLink to={'/account'}>
|
||||||
|
<FontAwesomeIcon icon={faUserCircle}/>
|
||||||
|
</NavLink>
|
||||||
|
</Tooltip>
|
||||||
{rootAdmin &&
|
{rootAdmin &&
|
||||||
<a href={'/admin'} rel={'noreferrer'}>
|
<Tooltip placement={'bottom'} content={'Admin'}>
|
||||||
<FontAwesomeIcon icon={faCogs}/>
|
<a href={'/admin'} rel={'noreferrer'}>
|
||||||
</a>
|
<FontAwesomeIcon icon={faCogs}/>
|
||||||
|
</a>
|
||||||
|
</Tooltip>
|
||||||
}
|
}
|
||||||
<button onClick={onTriggerLogout}>
|
<Tooltip placement={'bottom'} content={'Sign Out'}>
|
||||||
<FontAwesomeIcon icon={faSignOutAlt}/>
|
<button onClick={onTriggerLogout}>
|
||||||
</button>
|
<FontAwesomeIcon icon={faSignOutAlt}/>
|
||||||
|
</button>
|
||||||
|
</Tooltip>
|
||||||
</RightNavigation>
|
</RightNavigation>
|
||||||
</div>
|
</div>
|
||||||
</Navigation>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
import { faSearch } from '@fortawesome/free-solid-svg-icons';
|
import { faSearch } from '@fortawesome/free-solid-svg-icons';
|
||||||
import useEventListener from '@/plugins/useEventListener';
|
import useEventListener from '@/plugins/useEventListener';
|
||||||
import SearchModal from '@/components/dashboard/search/SearchModal';
|
import SearchModal from '@/components/dashboard/search/SearchModal';
|
||||||
|
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const [ visible, setVisible ] = useState(false);
|
const [ visible, setVisible ] = useState(false);
|
||||||
|
@ -24,9 +25,11 @@ export default () => {
|
||||||
onDismissed={() => setVisible(false)}
|
onDismissed={() => setVisible(false)}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
<div className={'navigation-link'} onClick={() => setVisible(true)}>
|
<Tooltip placement={'bottom'} content={'Search'}>
|
||||||
<FontAwesomeIcon icon={faSearch}/>
|
<div className={'navigation-link'} onClick={() => setVisible(true)}>
|
||||||
</div>
|
<FontAwesomeIcon icon={faSearch}/>
|
||||||
|
</div>
|
||||||
|
</Tooltip>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,7 +21,7 @@ const SubNavigation = styled.div`
|
||||||
|
|
||||||
&:active, &.active {
|
&:active, &.active {
|
||||||
${tw`text-neutral-100`};
|
${tw`text-neutral-100`};
|
||||||
box-shadow: inset 0 -2px ${theme`colors.cyan.700`.toString()};
|
box-shadow: inset 0 -2px ${theme`colors.cyan.600`.toString()};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,9 @@ import {
|
||||||
import { AnimatePresence, motion } from 'framer-motion';
|
import { AnimatePresence, motion } from 'framer-motion';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
|
rest?: number;
|
||||||
|
delay?: number | Partial<{ open: number; close: number }>;
|
||||||
|
alwaysOpen?: boolean;
|
||||||
content: string | React.ReactChild;
|
content: string | React.ReactChild;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
arrow?: boolean;
|
arrow?: boolean;
|
||||||
|
@ -34,22 +37,35 @@ const arrowSides: Record<Side, Side> = {
|
||||||
right: 'left',
|
right: 'left',
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ({ content, children, disabled = false, ...props }: Props) => {
|
export default ({
|
||||||
|
content,
|
||||||
|
children,
|
||||||
|
disabled = false,
|
||||||
|
alwaysOpen = false,
|
||||||
|
delay = { open: 500 },
|
||||||
|
rest = 30,
|
||||||
|
...props
|
||||||
|
}: Props) => {
|
||||||
const arrowEl = useRef<HTMLDivElement>(null);
|
const arrowEl = useRef<HTMLDivElement>(null);
|
||||||
const [ open, setOpen ] = useState(false);
|
const [ open, setOpen ] = useState(alwaysOpen || false);
|
||||||
|
|
||||||
const { x, y, reference, floating, middlewareData, strategy, context } = useFloating({
|
const { x, y, reference, floating, middlewareData, strategy, context } = useFloating({
|
||||||
open,
|
open,
|
||||||
placement: props.placement || 'top',
|
placement: props.placement || 'top',
|
||||||
strategy: props.strategy || 'absolute',
|
strategy: props.strategy || 'absolute',
|
||||||
middleware: [ offset(6), flip(), shift({ padding: 6 }), arrow({ element: arrowEl, padding: 6 }) ],
|
middleware: [
|
||||||
onOpenChange: setOpen,
|
offset(props.arrow ? 10 : 6),
|
||||||
|
flip(),
|
||||||
|
shift({ padding: 6 }),
|
||||||
|
arrow({ element: arrowEl, padding: 6 }),
|
||||||
|
],
|
||||||
|
onOpenChange: (o) => setOpen(o || alwaysOpen || false),
|
||||||
whileElementsMounted: autoUpdate,
|
whileElementsMounted: autoUpdate,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { getReferenceProps, getFloatingProps } = useInteractions([
|
const { getReferenceProps, getFloatingProps } = useInteractions([
|
||||||
useFocus(context),
|
useFocus(context),
|
||||||
useHover(context, { restMs: 30 }),
|
useHover(context, { restMs: rest, delay }),
|
||||||
useRole(context, { role: 'tooltip' }),
|
useRole(context, { role: 'tooltip' }),
|
||||||
useDismiss(context),
|
useDismiss(context),
|
||||||
]);
|
]);
|
||||||
|
@ -70,7 +86,7 @@ export default ({ content, children, disabled = false, ...props }: Props) => {
|
||||||
initial={{ opacity: 0, scale: 0.85 }}
|
initial={{ opacity: 0, scale: 0.85 }}
|
||||||
animate={{ opacity: 1, scale: 1 }}
|
animate={{ opacity: 1, scale: 1 }}
|
||||||
exit={{ opacity: 0 }}
|
exit={{ opacity: 0 }}
|
||||||
transition={{ type: 'easeIn', damping: 20, stiffness: 300, duration: 0.1 }}
|
transition={{ type: 'spring', damping: 20, stiffness: 300, duration: 0.075 }}
|
||||||
{...getFloatingProps({
|
{...getFloatingProps({
|
||||||
ref: floating,
|
ref: floating,
|
||||||
className: 'absolute top-0 left-0 bg-gray-900 text-sm text-gray-200 px-3 py-2 rounded pointer-events-none max-w-[90vw]',
|
className: 'absolute top-0 left-0 bg-gray-900 text-sm text-gray-200 px-3 py-2 rounded pointer-events-none max-w-[90vw]',
|
||||||
|
@ -86,10 +102,10 @@ export default ({ content, children, disabled = false, ...props }: Props) => {
|
||||||
<div
|
<div
|
||||||
ref={arrowEl}
|
ref={arrowEl}
|
||||||
style={{
|
style={{
|
||||||
transform: `translate(${Math.round(ax || 0)}px, ${Math.round(ay || 0)}px)`,
|
transform: `translate(${Math.round(ax || 0)}px, ${Math.round(ay || 0)}px) rotate(45deg)`,
|
||||||
[side]: '-6px',
|
[side]: '-6px',
|
||||||
}}
|
}}
|
||||||
className={'absolute top-0 left-0 bg-gray-900 w-3 h-3 rotate-45'}
|
className={'absolute top-0 left-0 bg-gray-900 w-3 h-3'}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
</motion.div>
|
</motion.div>
|
||||||
|
|
Loading…
Reference in a new issue