-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Truncate: Convert component to TypeScript #41697
Changes from all commits
13c4d3a
226d2dd
a88df9e
53523fe
6b6dc29
0433dfd
f47c5f6
03ed968
32913b9
82b7747
ee2d863
374d2f5
60eaa0b
bcd5675
7aae99c
2c8484e
9884300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Truncate } from '..'; | ||
|
||
const meta: ComponentMeta< typeof Truncate > = { | ||
component: Truncate, | ||
title: 'Components (Experimental)/Truncate', | ||
argTypes: { | ||
children: { control: { type: 'text' } }, | ||
as: { control: { type: 'text' } }, | ||
}, | ||
parameters: { | ||
controls: { | ||
expanded: true, | ||
}, | ||
docs: { source: { state: 'open' } }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
const defaultText = | ||
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis dictum tortor, eu tincidunt justo scelerisque tincidunt. Duis semper dui id augue malesuada, ut feugiat nisi aliquam. Vestibulum venenatis diam sem, finibus dictum massa semper in. Nulla facilisi. Nunc vulputate faucibus diam, in lobortis arcu ornare vel. In dignissim nunc sed facilisis finibus. Etiam imperdiet mattis arcu, sed rutrum sapien blandit gravida. Aenean sollicitudin neque eget enim blandit, sit amet rutrum leo vehicula. Nunc malesuada ultricies eros ut faucibus. Aliquam erat volutpat. Nulla nec feugiat risus. Vivamus iaculis dui aliquet ante ultricies feugiat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Vivamus nec pretium velit, sit amet consectetur ante. Praesent porttitor ex eget fermentum mattis.'; | ||
|
||
const Template: ComponentStory< typeof Truncate > = ( args ) => { | ||
return <Truncate { ...args } />; | ||
}; | ||
|
||
export const Default: ComponentStory< typeof Truncate > = Template.bind( {} ); | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Default.args = { | ||
numberOfLines: 2, | ||
children: defaultText, | ||
}; | ||
|
||
export const CharacterCount: ComponentStory< typeof Truncate > = Template.bind( | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{} | ||
); | ||
CharacterCount.args = { | ||
limit: 23, | ||
children: defaultText, | ||
ellipsizeMode: 'tail', | ||
ellipsis: '[---]', | ||
}; | ||
CharacterCount.storyName = 'Truncate by character count'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
export type TruncateEllisizeMode = 'auto' | 'head' | 'tail' | 'middle' | 'none'; | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { ReactNode } from 'react'; | ||
|
||
export interface Props { | ||
export type TruncateEllipsizeMode = | ||
| 'auto' | ||
| 'head' | ||
| 'tail' | ||
| 'middle' | ||
| 'none'; | ||
|
||
export type TruncateProps = { | ||
/** | ||
* The ellipsis string when `truncate` is set. | ||
* The ellipsis string when truncating the text by the `limit` prop's value. | ||
* | ||
* @default '...' | ||
* @default '…' | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
ellipsis?: string; | ||
/** | ||
* Determines where to truncate. For example, we can truncate text right in the middle. To do this, we need to set `ellipsizeMode` to `middle` and a text `limit`. | ||
* Determines where to truncate. For example, we can truncate text right in | ||
* the middle. To do this, we need to set `ellipsizeMode` to `middle` and a | ||
* text `limit`. | ||
* | ||
* * `auto`: Trims content at the end automatically without a `limit`. | ||
* * `head`: Trims content at the beginning. Requires a `limit`. | ||
|
@@ -17,19 +29,25 @@ export interface Props { | |
* | ||
* @default 'auto' | ||
*/ | ||
ellipsizeMode?: TruncateEllisizeMode; | ||
ellipsizeMode?: TruncateEllipsizeMode; | ||
/** | ||
* Determines the max characters when `truncate` is set. | ||
* Determines the max number of characters to be displayed before the rest | ||
* of the text gets truncated. Requires `ellipsizeMode` to assume values | ||
* different from `auto` and `none`. | ||
* | ||
* @default 0 | ||
*/ | ||
limit?: number; | ||
walbo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Clamps the text content to the specified `numberOfLines`, adding the `ellipsis` at the end. | ||
* Clamps the text content to the specified `numberOfLines`, adding an | ||
* ellipsis at the end. Note: this feature ignores the value of the | ||
* `ellipsis` prop and always displays the default `…` ellipsis. | ||
* | ||
* @default 0 | ||
*/ | ||
numberOfLines?: number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also realised that this type is not a great fit for Even a default of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL. Good to know the spec there. |
||
/** | ||
* The children elements. | ||
*/ | ||
children: React.ReactNode; | ||
} | ||
children: ReactNode; | ||
}; |
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.
Update: Sorry I almost forgot this was a TS migration PR. No runtime changes! 😂
Nit: I find this 👇 simpler to read, might just be me though.