Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components): [tabs] optimize SSR #15183

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/tabs/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface TabsRootContext {
props: TabsProps
currentName: Ref<string | number>
registerPane: (pane: TabsPaneContext) => void
sortPane: (pane: TabsPaneContext) => void
unregisterPane: (uid: number) => void
}

Expand Down
3 changes: 2 additions & 1 deletion packages/components/tabs/src/tab-pane.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ const pane = reactive({
isClosable,
})

tabsRoot.registerPane(pane)
onMounted(() => {
tabsRoot.registerPane(pane)
tabsRoot.sortPane(pane)
})

onUnmounted(() => {
Expand Down
42 changes: 26 additions & 16 deletions packages/components/tabs/src/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import TabNav from './tab-nav'

import type { TabNavInstance } from './tab-nav'
import type { TabsPaneContext } from './constants'
import type { ExtractPropTypes } from 'vue'
import type { ExtractPropTypes, FunctionalComponent, VNode } from 'vue'
import type { Awaitable } from '@element-plus/utils'

export type TabPaneName = string | number
Expand Down Expand Up @@ -90,7 +90,7 @@ const Tabs = defineComponent({

const {
children: panes,
addChild: registerPane,
addChild: sortPane,
removeChild: unregisterPane,
} = useOrderedChildren<TabsPaneContext>(getCurrentInstance()!, 'ElTabPane')

Expand Down Expand Up @@ -169,14 +169,21 @@ const Tabs = defineComponent({
provide(tabsRootContextKey, {
props,
currentName,
registerPane,
registerPane: (pane: TabsPaneContext) => {
panes.value.push(pane)
},
sortPane,
unregisterPane,
})

expose({
currentName,
})

const TabNavRenderer: FunctionalComponent<{ render: () => VNode }> = ({
render,
}) => {
return render()
}
return () => {
const addSlot = slots.addIcon
const newButton =
Expand All @@ -202,15 +209,19 @@ const Tabs = defineComponent({
const header = (
<div class={[ns.e('header'), ns.is(props.tabPosition)]}>
{newButton}
<TabNav
ref={nav$}
currentName={currentName.value}
editable={props.editable}
type={props.type}
panes={panes.value}
stretch={props.stretch}
onTabClick={handleTabClick}
onTabRemove={handleTabRemove}
<TabNavRenderer
render={() => (
<TabNav
ref={nav$}
currentName={currentName.value}
editable={props.editable}
type={props.type}
panes={panes.value}
stretch={props.stretch}
onTabClick={handleTabClick}
onTabRemove={handleTabRemove}
/>
)}
/>
</div>
)
Expand All @@ -230,9 +241,8 @@ const Tabs = defineComponent({
},
]}
>
{...props.tabPosition !== 'bottom'
? [header, panels]
: [panels, header]}
{panels}
{header}
</div>
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/hooks/use-ordered-children/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const useOrderedChildren = <T extends { uid: number }>(
const children: Record<number, T> = {}
const orderedChildren = shallowRef<T[]>([])

// TODO: split into two functions: addChild and sortChildren
const addChild = (child: T) => {
children[child.uid] = child
orderedChildren.value = getOrderedChildren(vm, childComponentName, children)
Expand Down
11 changes: 9 additions & 2 deletions packages/theme-chalk/src/tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

@include b(tabs) {
@include set-component-css-var('tabs', $tabs);
display: flex;

@include e(header) {
padding: 0;
Expand Down Expand Up @@ -167,6 +168,7 @@
@include e(content) {
overflow: hidden;
position: relative;
flex-grow: 1;
}
@include m(card) {
> .#{$namespace}-tabs__header {
Expand Down Expand Up @@ -317,6 +319,8 @@
}
}
@include m(bottom) {
flex-direction: column;

.#{$namespace}-tabs__header.is-bottom {
margin-bottom: 0;
margin-top: 10px;
Expand Down Expand Up @@ -413,8 +417,9 @@
}
}
@include m(left) {
flex-direction: row-reverse;

.#{$namespace}-tabs__header.is-left {
float: left;
margin-bottom: 0;
margin-right: 10px;
}
Expand Down Expand Up @@ -497,7 +502,6 @@
}
@include m(right) {
.#{$namespace}-tabs__header.is-right {
float: right;
margin-bottom: 0;
margin-left: 10px;
}
Expand Down Expand Up @@ -568,6 +572,9 @@
}
}
}
@include m(top) {
flex-direction: column-reverse;
}
}

.slideInRight-transition,
Expand Down