Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions packages/components/anchor/Anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@

export interface AnchorProps extends TdAnchorProps, StyledProps {
children?: React.ReactNode;
direction?: Direction;
}

type Direction = 'vertical' | 'horizontal';

type CursorStyle<T = Direction> = T extends 'vertical'
? { top: string; height?: string; opacity: number }
: T extends 'horizontal'
? { left: string; width?: string; opacity: number }

Check failure on line 31 in packages/components/anchor/Anchor.tsx

View workflow job for this annotation

GitHub Actions / test

Insert `··`
: never;

Check failure on line 32 in packages/components/anchor/Anchor.tsx

View workflow job for this annotation

GitHub Actions / test

Insert `··`

interface IntervalRef {
// 收集 anchor-item
items: string[];
Expand All @@ -47,13 +56,14 @@
onChange,
className,
getCurrentAnchor,
direction,
...rest
} = useDefaultProps(props, anchorDefaultProps);

const { classPrefix } = useConfig();

const [activeItem, setActiveItem] = useState<string>('');
const [cursorStyle, setCursorStyle] = useState<{ top: string; height?: string; opacity: number }>({
const [cursorStyle, setCursorStyle] = useState<CursorStyle>({
top: '0px',
height: '0px',
opacity: 0,
Expand All @@ -66,6 +76,8 @@
handleScrollLock: false,
});

const isHorizontal = direction === 'horizontal';

useImperativeHandle(ref, () => anchorEl.current);

/**
Expand Down Expand Up @@ -112,10 +124,13 @@
if (!pointEl) {
setCursorStyle(null);
} else {
const { offsetTop: top, offsetHeight: height } = pointEl;
setCursorStyle({ top: `${top}px`, height: `${height}px`, opacity: 1 });
const { offsetTop: top, offsetHeight: height, offsetLeft: left, offsetWidth: width } = pointEl;
const style = isHorizontal
? { left: `${left}px`, width: `${width}px`, opacity: 1 }
: { top: `${top}px`, height: `${height}px`, opacity: 1 };
setCursorStyle(style);
}
}, [activeItem, classPrefix]);
}, [activeItem, classPrefix, isHorizontal]);

const handleScroll = useCallback(() => {
const { scrollContainer, handleScrollLock } = intervalRef.current;
Expand Down Expand Up @@ -164,6 +179,7 @@
[`${classPrefix}-size-s`]: size === 'small',
[`${classPrefix}-size-m`]: size === 'medium',
[`${classPrefix}-size-l`]: size === 'large',
[`${classPrefix}-anchor__horizontal`]: isHorizontal,
},
className,
);
Expand Down Expand Up @@ -194,7 +210,30 @@
</AnchorContext.Provider>
);

return isEmpty(affixProps) ? Cmp : <Affix {...affixProps}>{Cmp}</Affix>;
return (
<>
<style>
{`
.t-anchor__horizontal{
display:flex;
width: fit-content;
}
.t-anchor__horizontal .t-anchor__line{
top:100%;
width:100%;
height:1px;
}
.t-anchor__horizontal .t-anchor__line-cursor-wrapper{
height:1px;
}
.t-anchor__horizontal .t-anchor__line-cursor-wrapper .t-anchor__line-cursor{
width:100%;
}
`}
</style>
{isEmpty(affixProps) ? Cmp : <Affix {...affixProps}>{Cmp}</Affix>}
</>
);
},
{
AnchorItem,
Expand Down
46 changes: 46 additions & 0 deletions packages/components/anchor/_example/container-horizontal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { Anchor } from 'tdesign-react';

import type { TdAnchorProps } from 'tdesign-react';

const { AnchorItem } = Anchor;

export default function AnchorAttach() {
const handleClick: TdAnchorProps['onClick'] = ({ e, href, title }) => {
e.preventDefault();
console.log('handleClick', href, title);
};

return (
<>
<div id="attach" className="anchor-demo-attach">
<Anchor direction="horizontal" container="#anchor-container" onClick={handleClick}>
<AnchorItem href="#content-1" title="content-1" />
<AnchorItem href="#content-2" title="content-2" />
<AnchorItem href="#content-3" title="content-3" />
<AnchorItem href="#content-4" title="content-4" />
</Anchor>
<div
id="anchor-container"
style={{ width: '100%', height: '200px', overflow: 'auto', textAlign: 'center', fontSize: '22px' }}
>
<div id="content-1" style={{ background: '#DFEFFF', lineHeight: '100px' }}>
content-1
</div>
<div id="content-2" style={{ background: '#BFDBF7', lineHeight: '100px' }}>
content-2
</div>
<div id="content-3" style={{ background: '#9BC5F2', lineHeight: '100px' }}>
content-3
</div>
<div id="content-4" style={{ background: '#7BAFED', lineHeight: '100px' }}>
content-4
</div>
<div id="content-5" style={{ background: '#5C99EB', lineHeight: '100px' }}>
content-5
</div>
</div>
</div>
</>
);
}
50 changes: 42 additions & 8 deletions packages/components/anchor/_example/cursor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,52 @@ const cursorStyle: React.CSSProperties = {
marginLeft: '-5px',
top: '50%',
marginTop: '-5px',
zIndex: 2,
};

export default function AnchorBase() {
return (
<div style={{ display: 'flex' }}>
<Anchor targetOffset={150} cursor={<div style={cursorStyle}></div>}>
<AnchorItem href="#基础锚点" title="基础锚点" />
<AnchorItem href="#多级锚点" title="多级锚点" />
<AnchorItem href="#指定容器锚点" title="指定容器锚点" />
<AnchorItem href="#特定交互锚点" title="特定交互锚点" />
<AnchorItem href="#尺寸" title="尺寸"></AnchorItem>
<>
<Anchor
direction="horizontal"
targetOffset={0}
container="#anchor-container"
cursor={<div style={cursorStyle}></div>}
>
<AnchorItem href="#content-1" title="基础锚点" />
<AnchorItem href="#content-2" title="多级锚点" />
<AnchorItem href="#content-3" title="指定容器锚点" />
<AnchorItem href="#content-4" title="特定交互锚点" />
</Anchor>
</div>
<div style={{ display: 'flex', marginTop: '10px' }}>
<Anchor targetOffset={0} container="#anchor-container" cursor={<div style={cursorStyle}></div>}>
<AnchorItem href="#content-1" title="基础锚点" />
<AnchorItem href="#content-2" title="多级锚点" />
<AnchorItem href="#content-3" title="指定容器锚点" />
<AnchorItem href="#content-4" title="特定交互锚点" />
</Anchor>

<div
id="anchor-container"
style={{ width: '100%', height: '200px', overflow: 'auto', textAlign: 'center', fontSize: '22px' }}
>
<div id="content-1" style={{ background: '#DFEFFF', lineHeight: '100px' }}>
content-1
</div>
<div id="content-2" style={{ background: '#BFDBF7', lineHeight: '100px' }}>
content-2
</div>
<div id="content-3" style={{ background: '#9BC5F2', lineHeight: '100px' }}>
content-3
</div>
<div id="content-4" style={{ background: '#7BAFED', lineHeight: '100px' }}>
content-4
</div>
<div id="content-5" style={{ background: '#5C99EB', lineHeight: '100px' }}>
content-5
</div>
</div>
</div>
</>
);
}
Loading
Loading