|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- import * as React from 'react'
- import * as PropTypes from 'prop-types'
- import Link from 'next/link'
- import styled from 'styled-components'
- import Icon from '../Icon/Icon'
-
- const NoteLink = styled('a')({
- display: 'flex',
- textDecoration: 'none',
- color: 'inherit',
- alignItems: 'center',
- position: 'relative',
- flex: 'auto',
- padding: '0 1rem',
- boxSizing: 'border-box',
- })
-
- const NoteLinkPrimary = styled('span')({
- display: 'block',
- flex: 'auto',
- })
-
- const NoteLinkTitle = styled('strong')({
- display: 'block',
- height: '1.25em',
- position: 'relative',
- })
-
- const NoteLinkTitleOverflow = styled('span')({
- whiteSpace: 'nowrap',
- overflow: 'hidden',
- position: 'absolute',
- top: 0,
- left: 0,
- width: '100%',
- height: '100%',
- textOverflow: 'ellipsis',
- })
-
- const LinkContainer = styled('div')({
- position: 'relative',
- color: 'var(--color-primary, blue)',
- display: 'flex',
- alignItems: 'stretch',
- height: '4rem',
- })
-
- const NoteActions = styled('div')({
- display: 'flex',
- alignItems: 'stretch',
- height: '100%',
- position: 'relative',
- '@media (min-width: 1080px)': {
- opacity: 0,
- [`${LinkContainer}:hover &`]: {
- opacity: 1,
- },
- },
- })
-
- const NoteAction = styled('button')({
- height: '100%',
- width: '4rem',
- background: 'transparent',
- border: 0,
- color: 'inherit',
- cursor: 'pointer',
- outline: 0,
- padding: 0,
- })
-
- const NoteLinkBackground = styled('span')({
- '::before': {
- content: "''",
- position: 'absolute',
- top: 0,
- left: 0,
- width: '0.25rem',
- height: '100%',
- display: 'block',
- backgroundColor: 'currentColor',
- },
- '::after': {
- content: "''",
- opacity: 0.125,
- backgroundColor: 'currentColor',
- top: 0,
- left: 0,
- width: '100%',
- height: '100%',
- position: 'absolute',
- },
- })
-
- const IconContainer = styled('span')({
- display: 'inline-block',
- verticalAlign: 'middle',
- marginRight: '0.5rem',
- })
-
- const PostMeta = styled('small')({
- opacity: 0.5,
- height: '1.25rem',
- display: 'block',
- lineHeight: 1.25,
- })
-
- const propTypes = {
- active: PropTypes.bool,
- href: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired,
- replace: PropTypes.bool,
- iconName: PropTypes.string,
- title: PropTypes.string,
- subtitle: PropTypes.node,
- actions: PropTypes.arrayOf(PropTypes.shape({
- id: PropTypes.string.isRequired,
- onClick: PropTypes.func,
- iconName: PropTypes.string,
- })),
- }
-
- type Props = PropTypes.InferProps<typeof propTypes>
-
- const SecondaryNavItem: React.FC<Props> = ({
- active = false,
- href,
- replace = false,
- iconName,
- title,
- subtitle,
- actions,
- }) => (
- <LinkContainer>
- {
- active
- && (
- <NoteLinkBackground />
- )
- }
- <Link
- href={href}
- replace={replace}
- passHref
- shallow
- >
- <NoteLink>
- <IconContainer>
- {
- iconName
- && (
- <Icon
- name={iconName}
- />
- )
- }
- </IconContainer>
- <NoteLinkPrimary>
- <NoteLinkTitle
- style={{ opacity: title.length > 0 ? 1 : 0.5, }}
- >
- <NoteLinkTitleOverflow>
- {title.length > 0 ? title : '(untitled)'}
- </NoteLinkTitleOverflow>
- </NoteLinkTitle>
- {
- subtitle
- && (
- <React.Fragment>
- {' '}
- <PostMeta>
- {subtitle}
- </PostMeta>
- </React.Fragment>
- )
- }
- </NoteLinkPrimary>
- </NoteLink>
- </Link>
- {
- Array.isArray(actions)
- && (
- <NoteActions>
- {actions.map(a => (
- <NoteAction
- key={a.id}
- onClick={a.onClick}
- >
- <Icon
- name={a.iconName}
- />
- </NoteAction>
- ))}
- </NoteActions>
- )
- }
- </LinkContainer>
- )
-
- SecondaryNavItem.propTypes = propTypes
-
- export default SecondaryNavItem
|