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

feat: tab.action&tab.extra #1339

Open
wants to merge 2 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/guide/page-tab.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,17 @@ export default (api: IApi) => {
};
```

```ts
// a.tsx
import React from 'react';

export default () => <>Hi, hello</>;

export const Extra = () => <>😄</>;

export const Action = () => <>🏀</>;

```

`component` 放入我们自定义的 Tab 内容,`test` 可以写入正则来匹配路由,这样我们就实现了为 `/componets` 下的路由页面添加自定义 Tab。
`component` 组件默认导出的是 Tab 页内容,`Extra` 和 `Action` 都是可选导出项,`Extra` 可以自定义每个 Tab 的紧跟内容,`Action` 可以自定义整个tab栏的右侧内容,同一路由下只有第一个 `Action` 有效。
4 changes: 4 additions & 0 deletions examples/normal/a.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import React from 'react';

export default () => <>Hi, hello</>;

export const Extra = () => <>😄</>;

export const Action = () => <>🏀</>;
72 changes: 41 additions & 31 deletions src/client/theme-default/slots/ContentTabs/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,58 @@
}
}

> li {
height: inherit;
&-nav {
flex: auto;
display: flex;
height: 60px;

> button {
padding: 0;
>li {
height: inherit;
color: @c-text-secondary;
font-size: 17px;
border: 0;
background: transparent;
cursor: pointer;
transition: all 0.2s;

&:hover {
color: @c-primary;
>button {
padding: 0;
height: inherit;
color: @c-text-secondary;
font-size: 17px;
border: 0;
background: transparent;
cursor: pointer;
transition: all 0.2s;

&:hover {
color: @c-primary;
}
}
}

&:not(last-child) {
margin-inline-end: 42px;
&:not(last-child) {
margin-inline-end: 42px;

@media @mobile {
margin-inline-end: 20px;
@media @mobile {
margin-inline-end: 20px;
}
}
}

&[data-active] {
position: relative;
&[data-active] {
position: relative;

> button {
color: @c-text;
}
>button {
color: @c-text;
}

&::after {
content: '';
position: absolute;
left: 0;
right: 0;
bottom: -1px;
height: 1px;
background-color: @c-primary;
&::after {
content: '';
position: absolute;
left: 0;
right: 0;
bottom: -1px;
height: 1px;
background-color: @c-primary;
}
}
}
}

&-action {
flex: none;
}
}
44 changes: 29 additions & 15 deletions src/client/theme-default/slots/ContentTabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import './index.less';

type IContentTabs = ReturnType<typeof useRouteMeta>['tabs'];

const TabsAction: FC<{ tabs: IContentTabs }> = ({ tabs }) => {
const tabActions = tabs
?.map((tab) => tab.components.Action)
.filter((tab) => tab);

return Boolean(tabActions?.length) ? (
<div className="dumi-default-content-tabs-actions">
{React.createElement(tabActions![0])}
</div>
) : null;
};

export interface IContentTabsProps {
tabs: IContentTabs;
tabKey: string | null;
Expand All @@ -17,24 +29,26 @@ const ContentTabs: FC<IContentTabsProps> = ({
}) => {
const intl = useIntl();

// TODO: tab.Extra & tab.Action render

return Boolean(tabs?.length) ? (
<ul className="dumi-default-content-tabs">
<li onClick={() => onChange()} data-active={!key || undefined}>
<button type="button">
{intl.formatMessage({ id: 'content.tabs.default' })}
</button>
</li>
{tabs!.map((tab) => (
<li
key={tab.key}
onClick={() => onChange(tab)}
data-active={key === tab.key || undefined}
>
<button type="button">{tab.meta.frontmatter.title}</button>
<div className="dumi-default-content-tabs-nav">
<li onClick={() => onChange()} data-active={!key || undefined}>
<button type="button">
{intl.formatMessage({ id: 'content.tabs.default' })}
</button>
</li>
))}
{tabs!.map((tab) => (
<li
key={tab.key}
onClick={() => onChange(tab)}
data-active={key === tab.key || undefined}
>
<button type="button">{tab.meta.frontmatter.title}</button>
{tab.components.Extra && React.createElement(tab.components.Extra)}
</li>
))}
</div>
<TabsAction tabs={tabs} />
</ul>
) : null;
};
Expand Down