Skip to content

Commit

Permalink
Added TextOverflow component.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Jan 14, 2024
1 parent 82135a9 commit f786dc7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-basics",
"version": "0.119.0",
"version": "0.120.0",
"description": "Everyday components for React",
"license": "MIT",
"repository": {
Expand Down
6 changes: 0 additions & 6 deletions src/components/common/Text.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@
.size-xs {
font-size: var(--font-size-xs);
}

.overflow {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
13 changes: 6 additions & 7 deletions src/components/common/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { ElementType } from 'react';
import { CommonProps } from 'components/types';
import classNames from 'classnames';
// eslint-disable-next-line css-modules/no-unused-class
import styles from './Text.module.css';

export interface TextProps extends CommonProps {
as?: ElementType;
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
overflow?: boolean;
}

export function Text(props: TextProps) {
const { className, children, size = '', overflow = false, ...domProps } = props;
const { className, children, as = 'span', size = '', ...domProps } = props;
const Tag = as;

return (
<span
{...domProps}
className={classNames(className, styles[`size-${size}`], { [styles.overflow]: overflow })}
>
<Tag {...domProps} className={classNames(className, styles[`size-${size}`])}>
{children}
</span>
</Tag>
);
}

Expand Down
11 changes: 11 additions & 0 deletions src/components/common/TextOverflow.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.wrapper {
flex: 1 1 100%;
min-width: 0;
}

.overflow {
display: block;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
20 changes: 20 additions & 0 deletions src/components/common/TextOverflow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ElementType } from 'react';
import classNames from 'classnames';
import { CommonProps } from 'components/types';
import styles from './TextOverflow.module.css';

export interface TextOverflowProps extends CommonProps {
as?: ElementType;
}

export function TextOverflow(props: TextOverflowProps) {
const { children, className, ...domProps } = props;

return (
<div {...domProps} className={classNames(styles.wrapper, className)}>
<div className={styles.overflow}>{children}</div>
</div>
);
}

export default TextOverflow;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export * from 'components/common/Icon';
export * from 'components/common/Item';
export * from 'components/common/Text';
export * from 'components/common/TextOverflow';
export * from 'components/context/ReactBasicsProvider';
export * from 'components/form/ActionForm';
export * from 'components/form/Form';
Expand Down
8 changes: 7 additions & 1 deletion src/stories/Text.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ const Template: StoryFn<typeof Text> = args => {

export const Basic = makeStory(Template, {
args: {
value: 'value',
children: 'Text',
},
});

export const Overflow = makeStory(Template, {
args: {
children: 'This is long text that should be cut off.',
overflow: true,
},
});
26 changes: 26 additions & 0 deletions src/stories/TextOverflow.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import { StoryFn, Meta } from '@storybook/react';
import { TextOverflow, Text } from '../index';
import { makeStory } from './utils';

export default {
title: 'Common/TextOverflow',
component: TextOverflow,
} as Meta<typeof TextOverflow>;

const Template: StoryFn<typeof Text> = args => {
return (
<div style={{ width: 140 }}>
<TextOverflow>
<Text {...args} />
</TextOverflow>
</div>
);
};

export const Basic = makeStory(Template, {
args: {
value: 'value',
children: 'This is long text that should be cut off.',
},
});

0 comments on commit f786dc7

Please sign in to comment.