-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docz-theme-default): add automatic table of content (#106)
* fix(docz-theme-default): sidebar min-width * chore(docz-theme-default): some changes on NotFound * fix(docz): is active logic on Link * feat(docz-theme-default): add automatic table of content * chore(docz-theme-default): add onClick of link on SmallLink
- Loading branch information
1 parent
9cffa3b
commit 6ce9e84
Showing
9 changed files
with
158 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/docz-theme-default/src/components/shared/Sidebar/Link.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import * as React from 'react' | ||
import { SFC } from 'react' | ||
import { Link as BaseLink, LinkProps as BaseLinkProps, Entry } from 'docz' | ||
import { NavHashLink } from 'react-router-hash-link' | ||
|
||
import styled, { css } from 'react-emotion' | ||
|
||
export const linkStyle = (p: any) => css` | ||
position: relative; | ||
display: block; | ||
margin: 6px 16px; | ||
font-weight: 600; | ||
color: ${p.theme.colors.sidebarText}; | ||
text-decoration: none; | ||
transition: color 0.2s; | ||
&:hover, | ||
&:visited { | ||
color: ${p.theme.colors.sidebarText}; | ||
} | ||
&:hover, | ||
&.active { | ||
color: ${p.theme.colors.primary}; | ||
font-weight: 600; | ||
} | ||
` | ||
|
||
const LinkStyled = styled(BaseLink)` | ||
${linkStyle}; | ||
` | ||
|
||
interface LinkWrapperProps { | ||
active: boolean | ||
theme?: any | ||
} | ||
|
||
const activeWrapper = (p: LinkWrapperProps) => css` | ||
&:after { | ||
position: absolute; | ||
display: block; | ||
content: ''; | ||
top: 0; | ||
left: 0; | ||
width: 2px; | ||
height: 100%; | ||
background: ${p.theme.colors.border}; | ||
} | ||
` | ||
|
||
const LinkWrapper = styled('div')` | ||
position: relative; | ||
${(p: LinkWrapperProps) => p.active && activeWrapper(p)}; | ||
` | ||
|
||
const isActive = (doc: Entry, location: any) => { | ||
return doc.route === location.pathname | ||
} | ||
|
||
const SmallLink = styled(NavHashLink)` | ||
font-size: 14px; | ||
padding: 0 0 5px 26px; | ||
text-decoration: none; | ||
opacity: 0.5; | ||
transition: opacity 0.2s; | ||
&, | ||
&:visited, | ||
&.active { | ||
color: ${p => p.theme.colors.sidebarText}; | ||
} | ||
&.active { | ||
opacity: 1; | ||
} | ||
` | ||
|
||
const Submenu = styled('div')` | ||
display: flex; | ||
flex-direction: column; | ||
margin: 5px 0; | ||
` | ||
|
||
const isSmallLinkActive = (slug: string) => (m: any, location: any) => | ||
slug === location.hash.slice(1, Infinity) | ||
|
||
interface LinkProps extends BaseLinkProps { | ||
doc: Entry | ||
} | ||
|
||
export const Link: SFC<LinkProps> = ({ doc, onClick, ...props }) => { | ||
const active = isActive(doc, location) | ||
|
||
return ( | ||
<LinkWrapper active={active}> | ||
<LinkStyled {...props} onClick={onClick} /> | ||
{active && ( | ||
<Submenu> | ||
{doc.headings.map( | ||
heading => | ||
heading.depth > 1 && | ||
heading.depth < 3 && ( | ||
<SmallLink | ||
key={heading.slug} | ||
onClick={onClick} | ||
to={{ pathname: doc.route, hash: heading.slug }} | ||
isActive={isSmallLinkActive(heading.slug)} | ||
> | ||
{heading.value} | ||
</SmallLink> | ||
) | ||
)} | ||
</Submenu> | ||
)} | ||
</LinkWrapper> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
import * as React from 'react' | ||
import { SFC } from 'react' | ||
import { NavLink, NavLinkProps } from 'react-router-dom' | ||
import { NavLink, NavLinkProps as LinkProps } from 'react-router-dom' | ||
|
||
export const isActive = (match: any, location: any) => | ||
match && match.url === location.pathname | ||
export const Link: SFC<LinkProps> = props => <NavLink {...props} exact={true} /> | ||
|
||
export const Link: SFC<NavLinkProps> = props => ( | ||
<NavLink isActive={isActive} {...props} /> | ||
) | ||
export { LinkProps } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export { theme, Entry } from './theme' | ||
export { DocPreview, PageProps, RenderComponent } from './components/DocPreview' | ||
export { Docs, DocsRenderProps } from './components/Docs' | ||
export { Link } from './components/Link' | ||
export { Link, LinkProps } from './components/Link' | ||
export { Playground } from './components/Playground' | ||
export { PropsTable } from './components/PropsTable' | ||
export { ThemeConfig } from './components/ThemeConfig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters