fix(segment): preserve fit-content indicator animation

This commit is contained in:
XiaoDaiGua-Ray 2026-11-04 10:30:28 +08:00
parent dfa9fdbec2
commit cee3f01cf9
4 changed files with 124 additions and 184 deletions

View File

@ -0,0 +1,7 @@
# RSegment
`width` 支持 `fitContent`、`block` 和像素数值。示例见 [分段器试验台](../../views/demo/segment/index.tsx)。
在 Naive UI 2.45.3 中,选中态默认加粗。轨道使用内容宽度时,尤其是标签长短不同或带图标、提示时,切换选中态会改变轨道宽度。NTabs 的轨道 ResizeObserver 随即执行 `handleSegmentResize`,临时加上 `transition-disabled` 并强制同步胶囊位置。这个同步会让刚开始的位移动画跳到终点。
`fitContent` 模式固定各标签的字重,用颜色和胶囊标示选中态。内容过长时导航区可以横向滚动,避免撑破外层。轨道真正发生尺寸变化时继续让 NTabs 同步位置。减少动态效果偏好下取消胶囊过渡。

View File

@ -92,7 +92,10 @@ export default defineComponent({
<NTabs
{...($props as TabsProps)}
style={[cssVars]}
class="r-segment"
class={[
'r-segment',
{ 'r-segment--intrinsic': this.width === 'fitContent' },
]}
type="segment"
animated
themeOverrides={Object.assign({}, themeOverrides, _themeOverrides)}

View File

@ -1,3 +1,30 @@
.r-segment {
width: var(--r-segment-width);
max-inline-size: 100%;
}
.r-segment--intrinsic {
.n-tabs-nav {
overflow-x: auto;
}
.n-tabs-rail {
inline-size: max-content;
min-inline-size: 100%;
}
.n-tabs-tab-wrapper {
flex: 0 0 auto;
}
// 选中前后字重相同,避免 intrinsic rail 触发 Naive UI 的无动画 resize 同步。
.n-tabs-tab {
font-weight: var(--n-font-weight-strong);
}
}
@media (prefers-reduced-motion: reduce) {
.r-segment .n-tabs-rail .n-tabs-capsule {
transition-duration: 0s;
}
}

View File

@ -1,197 +1,100 @@
import { RIcon, RSegment } from '@/components'
import type {
RSegmentOptions,
RSegmentPopover,
RSegmentWidth,
} from '@/components'
import type { RSegmentOptions, RSegmentWidth } from '@/components'
import { useI18n } from '@/hooks'
import type { ComponentSize } from '@/types'
import {
NAlert,
NCard,
NFlex,
NGrid,
NGridItem,
NRadio,
NRadioGroup,
} from 'naive-ui'
import { NCard, NFlex, NRadioButton, NRadioGroup } from 'naive-ui'
import DemoPage from '../components/DemoPage'
export default defineComponent({
name: 'SegmentDemo',
setup() {
const baseOptions = [
{
label: '这是一',
key: 'this is one',
},
{
label: '这是二',
key: 'this is two',
},
{
label: '这是三',
key: 'this is three',
},
]
const options = [
...baseOptions,
{
label: '这是四',
key: 'this is four',
slots: {
default: () => '我是自定义',
},
},
]
const disabledOptions: RSegmentOptions[] = [
...baseOptions,
{
label: '不许点',
key: 'disabled option',
disabled: true,
},
{
label: 'popover',
key: 'popover',
popover: '我是 popover',
},
{
label: 'popover options',
key: 'popover options',
popover: {
label: '我是配置项的 popover',
},
},
]
const iconOptions = [
{
label: '设置',
key: 'setting',
icon: <RIcon name="action/setting" />,
},
{
label: '刷新',
key: 'reload',
icon: <RIcon name="action/reload" />,
},
{
label: '搜索',
key: 'search',
icon: <RIcon name="action/search" />,
popover: '我是搜索',
},
]
const segmentValueOne = ref('this is three')
const { t } = useI18n('showcase')
const value = ref<string | number>('overview')
const size = ref<ComponentSize>('medium')
const width = ref<RSegmentWidth>('block')
const width = ref<RSegmentWidth>('fitContent')
const options = computed<RSegmentOptions[]>(() => [
{
label: t('segmentOverview'),
key: 'overview',
icon: () => <RIcon name="navigation/dashboard" />,
},
{
label: t('segmentReports'),
key: 'reports',
popover: t('segmentPopover'),
},
{ label: t('segmentDisabled'), key: 'disabled', disabled: true },
])
const segmentChange = (value: string | number) => {
window.$message.info(String(value))
}
return {
baseOptions,
options,
segmentValueOne,
segmentChange,
size,
width,
disabledOptions,
iconOptions,
}
},
render() {
const { options, baseOptions, disabledOptions, iconOptions } = this
const { segmentChange } = this
return (
<NFlex vertical>
<NAlert title="警告⚠️" type="warning">
n-tabs 默认情况下会从 slot 获取默认展示的 tab 值,因此会产生一个 vue
slot 的 warning。如果你不想看到这个 warning,请设定一个
default-value。
</NAlert>
<NAlert title="已知问题" type="warning">
由于 NTabs 设计缘故,手动设置 width 为 fitContent 后,配置了 disabled,
popover 的配置项,会丢失过度效果。具体可以看下面的例子。
<br />
解决方法,在该组件外层包一层 div,设置宽度,但是不能为 fit-content。
</NAlert>
<NCard title="分段器">
<h3>
该组件基于 NTabs + NTabPane 封装,所以保留了一些该组件的特性。
</h3>
</NCard>
<NGrid cols={2} xGap={8} yGap={12}>
<NGridItem span={1}>
<NCard title="基础非受控分段器">
<RSegment defaultValue="this is two" options={baseOptions} />
</NCard>
</NGridItem>
<NGridItem span={1}>
<NCard title="自定义分段器插槽">
<RSegment defaultValue={this.segmentValueOne} options={options} />
</NCard>
</NGridItem>
<NGridItem span={1}>
<NCard title="updateValue 事件">
<RSegment
v-model:value={this.segmentValueOne}
defaultValue="this is two"
options={disabledOptions}
onUpdateValue={segmentChange.bind(this)}
width="block"
/>
</NCard>
</NGridItem>
<NGridItem span={1}>
<NCard title="自适应宽度">
<RSegment
defaultValue="setting"
options={iconOptions}
width="block"
/>
</NCard>
</NGridItem>
<NGridItem span={2}>
<NCard title="尺寸">
<NFlex vertical size="large">
<NRadioGroup v-model:value={this.size}>
<NRadio value="small">小</NRadio>
<NRadio value="medium">不小</NRadio>
<NRadio value="large">不太小</NRadio>
return () => (
<DemoPage
title={t('segmentTitle')}
description={t('segmentDescription')}
eyebrow="RAY TEMPLATE / EXAMPLES"
>
<div class="r-demo-grid">
<NCard class="r-demo-grid__wide" title={t('segmentPlayground')}>
<NFlex vertical size="large">
<NFlex align="center">
<NRadioGroup
v-model:value={width.value}
aria-label={t('segmentWidth')}
>
<NRadioButton value="fitContent">fitContent</NRadioButton>
<NRadioButton value="block">block</NRadioButton>
<NRadioButton value={480}>480px</NRadioButton>
</NRadioGroup>
<RSegment
defaultValue="this is two"
options={baseOptions}
size={this.size}
width="block"
/>
</NFlex>
</NCard>
</NGridItem>
<NGridItem span={2}>
<NCard title="宽度">
<NFlex vertical size="large">
<NAlert title="注意" type="info">
RSegment 组件宽度默认为
fitContent,按照内容自适应宽度;如果需要自适应宽度,请设置
width 为 block;如果需要手动指定宽度请设置一个数字。
</NAlert>
<NRadioGroup v-model:value={this.width}>
<NRadio value="fitContent">fitContent</NRadio>
<NRadio value="block">block</NRadio>
<NRadio value={800}>800</NRadio>
<NRadioGroup
v-model:value={size.value}
aria-label={t('segmentSize')}
>
<NRadioButton value="small">S</NRadioButton>
<NRadioButton value="medium">M</NRadioButton>
<NRadioButton value="large">L</NRadioButton>
</NRadioGroup>
<RSegment
defaultValue="this is two"
options={disabledOptions}
width={this.width}
/>
</NFlex>
</NCard>
</NGridItem>
</NGrid>
</NFlex>
<div class="r-demo-stage">
<RSegment
v-model:value={value.value}
options={options.value}
width={width.value}
size={size.value}
/>
</div>
<output>
{t('segmentSelected', {
value:
options.value.find(({ key }) => key === value.value)
?.label ?? value.value,
})}
</output>
</NFlex>
</NCard>
<NCard title={t('segmentUncontrolled')}>
<RSegment
defaultValue="overview"
options={options.value.slice(0, 2)}
/>
</NCard>
<NCard title={t('segmentCustom')}>
<RSegment
defaultValue="overview"
options={[
options.value[0],
{
key: 'custom',
label: h(
'span',
{ style: { fontVariantNumeric: 'tabular-nums' } },
t('segmentCustomLabel'),
),
},
]}
/>
</NCard>
</div>
</DemoPage>
)
},
})