forked from vison123/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ close ant-design#3557
- Loading branch information
Showing
12 changed files
with
328 additions
and
133 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
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,20 +1,72 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
import AnchorHelper, { scrollTo } from './anchorHelper'; | ||
|
||
export interface AnchorLinkProps { | ||
href: string; | ||
onClick: (href: string) => {}; | ||
active: boolean; | ||
onClick: (href: string) => void; | ||
active?: boolean; | ||
prefixCls?: string; | ||
children?: any; | ||
title?: Element; | ||
bounds: number; | ||
target?: () => HTMLElement | Window; | ||
} | ||
|
||
export default function AnchorLink(props) { | ||
const { prefixCls, active, href, children, onClick } = props; | ||
const cls = classnames({ | ||
[`${prefixCls}-link`]: true, | ||
[`${prefixCls}-link-active`]: active, | ||
}); | ||
export default class AnchorLink extends React.Component<AnchorLinkProps, any> { | ||
static contextTypes = { | ||
anchorHelper: React.PropTypes.any, | ||
}; | ||
|
||
return <div className={cls} onClick={() => onClick(props.href)} > | ||
{children} | ||
static childContextTypes = { | ||
anchorHelper: React.PropTypes.any, | ||
}; | ||
|
||
static defaultProps = { | ||
href: '#', | ||
prefixCls: 'ant-anchor', | ||
}; | ||
|
||
context: { | ||
anchorHelper: AnchorHelper; | ||
}; | ||
|
||
constructor(props, context) { | ||
super(props, context); | ||
} | ||
|
||
getChildContext() { | ||
return { | ||
anchorHelper: this.context.anchorHelper, | ||
}; | ||
} | ||
renderAnchorLink = (child) => { | ||
const { href } = child.props; | ||
if (href) { | ||
this.context.anchorHelper.addLink(href); | ||
return React.cloneElement(child, { | ||
onClick: this.context.anchorHelper.scrollTo, | ||
prefixCls: this.props.prefixCls, | ||
}); | ||
} | ||
return child; | ||
} | ||
render() { | ||
const { prefixCls, href, children, onClick, title, bounds } = this.props; | ||
const { anchorHelper } = this.context; | ||
const active = anchorHelper && anchorHelper.getCurrentAnchor(bounds) === href; | ||
const cls = classnames({ | ||
[`${prefixCls}-link`]: true, | ||
[`${prefixCls}-link-active`]: active, | ||
}); | ||
const scrollToFn = anchorHelper ? anchorHelper.scrollTo : scrollTo; | ||
return <div className={cls}> | ||
<span | ||
ref={(component) => component && active && anchorHelper ? anchorHelper.setActiveAnchor(component) : null} | ||
className={`${prefixCls}-link-title`} | ||
onClick={() => onClick ? onClick(href) : scrollToFn(href)} | ||
>{title}</span> | ||
{React.Children.map(children, this.renderAnchorLink)} | ||
</div>; | ||
} | ||
} | ||
} |
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,98 @@ | ||
import getScroll from '../_util/getScroll'; | ||
import getRequestAnimationFrame from '../_util/getRequestAnimationFrame'; | ||
|
||
export const reqAnimFrame = getRequestAnimationFrame(); | ||
|
||
export const easeInOutCubic = (t, b, c, d) => { | ||
t /= d/2; | ||
if (t < 1) return c/2*t*t*t + b; | ||
t -= 2; | ||
return c/2*(t*t*t + 2) + b; | ||
}; | ||
|
||
export function getDefaultTarget() { | ||
return typeof window !== 'undefined' ? | ||
window : null; | ||
} | ||
|
||
export function getOffsetTop(element): number { | ||
if (!element) { | ||
return 0; | ||
} | ||
|
||
if (!element.getClientRects().length) { | ||
return 0; | ||
} | ||
|
||
const rect = element.getBoundingClientRect(); | ||
|
||
if ( rect.width || rect.height ) { | ||
const doc = element.ownerDocument; | ||
const docElem = doc.documentElement; | ||
return rect.top - docElem.clientTop; | ||
} | ||
|
||
return rect.top; | ||
} | ||
|
||
export function scrollTo(href, target = getDefaultTarget){ | ||
const scrollTop = getScroll(target(), true); | ||
const offsetTop = getOffsetTop(document.querySelector(href)); | ||
const startTime = Date.now(); | ||
const frameFunc = () => { | ||
const timestamp = Date.now(); | ||
const time = timestamp - startTime; | ||
document.body.scrollTop = easeInOutCubic(time, scrollTop, offsetTop, 450); | ||
if (time < 450) { | ||
reqAnimFrame(frameFunc); | ||
} | ||
}; | ||
reqAnimFrame(frameFunc); | ||
history.pushState(null, undefined, href); | ||
} | ||
|
||
class AnchorHelper { | ||
private links: Array<string>; | ||
private currentAnchor: HTMLElement | null; | ||
|
||
constructor() { | ||
this.links = []; | ||
this.currentAnchor = null; | ||
} | ||
|
||
addLink(link) { | ||
if (this.links.indexOf(link) === -1) { | ||
this.links.push(link); | ||
} | ||
} | ||
|
||
getCurrentActiveAnchor(): HTMLElement | null { | ||
return this.currentAnchor; | ||
} | ||
|
||
setActiveAnchor(component) { | ||
this.currentAnchor = component; | ||
} | ||
|
||
getCurrentAnchor(bounds = 5) { | ||
let activeAnchor = ''; | ||
this.links.forEach(section => { | ||
const target = document.querySelector(section); | ||
if (target) { | ||
const top = getOffsetTop(target); | ||
const bottom = top + target.clientHeight; | ||
if ((top <= bounds) && (bottom >= -bounds)) { | ||
activeAnchor = section; | ||
} | ||
} | ||
}); | ||
return activeAnchor; | ||
} | ||
|
||
scrollTo(href, target = getDefaultTarget){ | ||
scrollTo(href, target); | ||
} | ||
} | ||
|
||
// const anchorHelper = new AnchorHelper(); | ||
export default AnchorHelper; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
order: 1 | ||
title: | ||
zh-CN: 嵌套的 AnchorLink | ||
en-US: Mixed AnchorLink | ||
--- | ||
|
||
## zh-CN | ||
|
||
最简单的用法。 | ||
|
||
## en-US | ||
|
||
The simplest usage. | ||
|
||
```jsx | ||
import { Anchor } from 'antd'; | ||
const { AnchorLink } = Anchor; | ||
|
||
ReactDOM.render( | ||
<Anchor offsetTop={100}> | ||
<AnchorLink href="#components-anchor-demo-basic" title="基本"> | ||
<AnchorLink href="#components-anchor-demo-mix" title="嵌套的 AnchorLink" /> | ||
</AnchorLink> | ||
<AnchorLink href="#components-anchor-demo-independ" title="独立使用" /> | ||
</Anchor> | ||
, mountNode); | ||
``` |
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
Oops, something went wrong.