-
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom indicator length (#671)
* feat: custom indicator length * fix: useEffect deps * test: add test case * chore: rename * chore: update test case
- Loading branch information
Showing
8 changed files
with
157 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ es | |
coverage | ||
yarn.lock | ||
package-lock.json | ||
pnpm-lock.yaml | ||
|
||
# umi | ||
.umi | ||
|
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,8 @@ | ||
--- | ||
title: Indicator | ||
nav: | ||
title: Demo | ||
path: /demo | ||
--- | ||
|
||
<code src="../examples/indicator.tsx"></code> |
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,35 @@ | ||
import React from 'react'; | ||
import Tabs from'../../src'; | ||
import '../../assets/index.less'; | ||
|
||
export default () => { | ||
const [destroy, setDestroy] = React.useState(false); | ||
const [items, setItems] = React.useState([ | ||
{ | ||
label: 'Light', | ||
key: 'light', | ||
children: 'Light!', | ||
}, | ||
{ | ||
label: 'Bamboo', | ||
key: 'bamboo', | ||
children: 'Bamboo!', | ||
}, | ||
{ | ||
label: 'Cute', | ||
key: 'cute', | ||
children: 'Cute!', | ||
disabled: true, | ||
}, | ||
]); | ||
|
||
if (destroy) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<React.StrictMode> | ||
<Tabs tabBarExtraContent="extra" items={items} getIndicatorLength={(origin) => origin - 16} /> | ||
</React.StrictMode> | ||
); | ||
}; |
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,73 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import raf from 'rc-util/lib/raf'; | ||
import { TabOffset } from '../interface'; | ||
|
||
export type GetIndicatorLength = number | ((origin: number) => number); | ||
|
||
export type UseIndicator = (options: { | ||
activeTabOffset: TabOffset, | ||
horizontal: boolean; | ||
rtl: boolean; | ||
indicatorLength: GetIndicatorLength; | ||
}) => { | ||
style: React.CSSProperties; | ||
} | ||
|
||
const useIndicator: UseIndicator = ({ | ||
activeTabOffset, | ||
horizontal, | ||
rtl, | ||
indicatorLength, | ||
}) => { | ||
const [inkStyle, setInkStyle] = useState<React.CSSProperties>(); | ||
const inkBarRafRef = useRef<number>(); | ||
|
||
const getLength = (origin: number) => { | ||
if (typeof indicatorLength === 'function') { | ||
return indicatorLength(origin); | ||
} | ||
if (typeof indicatorLength === 'number') { | ||
return indicatorLength; | ||
} | ||
return origin; | ||
} | ||
|
||
// Delay set ink style to avoid remove tab blink | ||
function cleanInkBarRaf() { | ||
raf.cancel(inkBarRafRef.current); | ||
} | ||
|
||
useEffect(() => { | ||
const newInkStyle: React.CSSProperties = {}; | ||
|
||
if (activeTabOffset) { | ||
if (horizontal) { | ||
if (rtl) { | ||
newInkStyle.right = activeTabOffset.right + activeTabOffset.width / 2; | ||
newInkStyle.transform = 'translateX(50%)'; | ||
} else { | ||
newInkStyle.left = activeTabOffset.left + activeTabOffset.width / 2; | ||
newInkStyle.transform = 'translateX(-50%)'; | ||
} | ||
newInkStyle.width = getLength(activeTabOffset.width); | ||
} else { | ||
newInkStyle.top = activeTabOffset.top + activeTabOffset.height / 2; | ||
newInkStyle.transform = 'translateY(-50%)'; | ||
newInkStyle.height = getLength(activeTabOffset.height); | ||
} | ||
} | ||
|
||
cleanInkBarRaf(); | ||
inkBarRafRef.current = raf(() => { | ||
setInkStyle(newInkStyle); | ||
}); | ||
|
||
return cleanInkBarRaf; | ||
}, [activeTabOffset, horizontal, rtl, indicatorLength]); | ||
|
||
return { | ||
style: inkStyle, | ||
} | ||
} | ||
|
||
export default useIndicator; |
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
b9902be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
tabs – ./
tabs-git-master-react-component.vercel.app
tabs-react-component.vercel.app