-
Notifications
You must be signed in to change notification settings - Fork 6
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: add Typography component #15
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
import { StoryBook, useControls, useCreateStore } from '@lobehub/ui'; | ||
import type { TypographyProps } from '@yuntijs/ui'; | ||
import { Typography } from '@yuntijs/ui'; | ||
|
||
export default () => { | ||
const store = useCreateStore(); | ||
const control: TypographyProps | any = useControls( | ||
{ | ||
time: '2024-04-15', | ||
format: 'YYYY-MM-DD hh:mm:ss', | ||
relativeTime: true, | ||
}, | ||
{ store } | ||
); | ||
return ( | ||
<StoryBook levaStore={store}> | ||
<Typography.Time {...control} /> | ||
</StoryBook> | ||
); | ||
}; |
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,43 @@ | ||
import { Space, Typography } from '@yuntijs/ui'; | ||
|
||
export default () => { | ||
return ( | ||
<Space direction="vertical"> | ||
<Typography.Link>Link</Typography.Link> | ||
<Typography.Text>Text</Typography.Text> | ||
<Typography.Title level={3}>Title</Typography.Title> | ||
<Typography.Paragraph> | ||
Paragraph:The YuntiUI components are inspired by LobeUI and developed based on Antd | ||
components, fully compatible with Antd components, and it is recommended to use antd-style | ||
as the default css-in-js styling solution. | ||
</Typography.Paragraph> | ||
<Typography.Time format="YYYY-MM-DD hh:mm:ss" relativeTime={true} time="2024-04-15" /> | ||
<Typography.Time format="YYYY-MM-DD hh:mm:ss" relativeTime={false} time="2024-04-15" /> | ||
<Typography.Time format="YYYY-MM-DD" relativeTime={false} time="2024-04-15" /> | ||
<Typography.Time | ||
format="YYYY-MM-DD" | ||
relativeTime={false} | ||
time="2024-04-15" | ||
tooltip={{ title: undefined }} | ||
/> | ||
<Typography.Time | ||
format="YYYY-MM-DD" | ||
relativeTime={false} | ||
time="2024-04-15" | ||
tooltip={{ title: '2024-04-15 15:00' }} | ||
/> | ||
<Typography.Time | ||
ellipsis={{ | ||
tooltip: { | ||
title: '2024-04-15 15:00', | ||
}, | ||
}} | ||
format="YYYY-MM-DD" | ||
relativeTime={false} | ||
style={{ width: 50 }} | ||
time="2024-04-15" | ||
tooltip={{ title: undefined }} | ||
/> | ||
</Space> | ||
); | ||
}; |
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,44 @@ | ||
--- | ||
nav: Components | ||
group: General | ||
title: Typography | ||
description: Basic text writing, including headings, body text, lists, and more. | ||
--- | ||
|
||
## Usage | ||
|
||
based on antd [Typography](https://ant.design/components/typography-cn/) component. | ||
|
||
### Simple usage | ||
|
||
```jsx | pure | ||
import { Space, Typography } from '@yuntijs/ui'; | ||
|
||
export default () => { | ||
return ( | ||
<Space direction="vertical"> | ||
<Typography.Link>Link</Typography.Link> | ||
<Typography.Text>Text</Typography.Text> | ||
<Typography.Title level={3}>Title</Typography.Title> | ||
<Typography.Paragraph> | ||
Paragraph:The YuntiUI components are inspired by LobeUI and developed based on Antd | ||
components, fully compatible with Antd components, and it is recommended to use antd-style | ||
as the default css-in-js styling solution. | ||
</Typography.Paragraph> | ||
<Typography.Time time="2024-04-15" format="YYYY-MM-DD hh:mm:ss" relativeTime={true} /> | ||
<Typography.Time time="2024-04-15" format="YYYY-MM-DD hh:mm:ss" relativeTime={false} /> | ||
<Typography.Time format="YYYY-MM-DD" relativeTime={false} time="2024-04-15" /> | ||
</Space> | ||
); | ||
}; | ||
``` | ||
|
||
<code src="./demos/index.tsx" center></code> | ||
|
||
## Playground | ||
|
||
<code src="./demos/Playground.tsx" nopadding></code> | ||
|
||
## APIs | ||
|
||
<API></API> |
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,103 @@ | ||
import { Typography as AntdTypography, Tooltip } from 'antd'; | ||
import { TextProps } from 'antd/es/typography/Text'; | ||
import { TooltipPropsWithTitle } from 'antd/lib/tooltip'; | ||
import dayjs from 'dayjs'; | ||
import dayjsRelativeTime from 'dayjs/plugin/relativeTime'; | ||
import { set } from 'lodash-es'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
dayjs.extend(dayjsRelativeTime); | ||
|
||
export const Typography = AntdTypography as TypographyProps; | ||
|
||
interface TimeProps extends TextProps { | ||
/** | ||
* @description Set display time | ||
* @default '-' | ||
*/ | ||
time: string; | ||
/** | ||
* @description Formatted display time | ||
* @default 'YYYY-MM-DD HH:mm:ss' | ||
*/ | ||
format?: string; | ||
/** | ||
* @description Display relative time | ||
* @default 'true' | ||
*/ | ||
relativeTime?: boolean; | ||
/** | ||
* @description Mouse above to show time | ||
* @default '{title: "YYYY-MM-DD HH:mm:ss"}' | ||
*/ | ||
tooltip?: TooltipPropsWithTitle; | ||
} | ||
const getFromNow = (t: any) => dayjs(t ? new Date(t) : new Date()).fromNow(); | ||
const Time: React.FC<TimeProps> = props => { | ||
const { time, format, relativeTime = true, tooltip, ...textProps } = props; | ||
const [showTime, setShowTime] = useState(getFromNow(time)); | ||
const [timer, setTimer] = useState<any>(); | ||
|
||
const clearTimer = () => { | ||
if (timer) { | ||
clearInterval(timer); | ||
} | ||
}; | ||
|
||
// The relative time within the last hour is updated automatically | ||
const setTimeInterval = (currentTime: dayjs.ConfigType) => { | ||
clearTimer(); | ||
const now = dayjs(); | ||
const timeMoment = dayjs(currentTime); | ||
const diff = now.diff(timeMoment); | ||
if (diff > 0 && diff < 60 * 60 * 1000) { | ||
const t = setInterval(() => { | ||
setShowTime(getFromNow(currentTime)); | ||
}, 60 * 1000); | ||
setTimer(t); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (relativeTime) { | ||
setTimeInterval(new Date(time)); | ||
} | ||
return () => { | ||
clearTimer(); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!relativeTime) return; | ||
const nextFromNow = getFromNow(time); | ||
if (nextFromNow !== showTime) { | ||
setShowTime(nextFromNow); | ||
setTimeInterval(new Date(time)); | ||
} | ||
}, [time, relativeTime]); | ||
|
||
const fmtTime = dayjs(time).format(format || 'YYYY-MM-DD HH:mm:ss'); | ||
|
||
const currentTime = relativeTime ? showTime : fmtTime; | ||
|
||
const tooltipTitle = tooltip?.title ?? (relativeTime ? fmtTime : undefined); | ||
|
||
// Avoid duplicate tooltips | ||
if (tooltipTitle) { | ||
set(textProps, 'ellipsis.tooltip.title', undefined); | ||
} | ||
|
||
return ( | ||
<Tooltip {...(tooltip || {})} title={tooltipTitle}> | ||
<Typography.Text {...textProps}>{currentTime}</Typography.Text> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export type TypographyProps = typeof AntdTypography & { | ||
Time: typeof Time; | ||
}; | ||
|
||
Typography.Time = Time; | ||
|
||
export default Typography; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
自定义的组件,最基本的属性要支持,比如
className
及style
等,建议基于 Text 扩展下,别直接用 span