Skip to content

Commit

Permalink
feat: text overflow and text aligns
Browse files Browse the repository at this point in the history
  • Loading branch information
maxholman committed Dec 11, 2022
1 parent bac9e3f commit 09618cd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
37 changes: 36 additions & 1 deletion lib/core.css.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { styleVariants } from '@vanilla-extract/css';
import { style, StyleRule, styleVariants } from '@vanilla-extract/css';
import { genericVars } from './design-system.css.js';

export type TextAlign = 'start' | 'end' | 'center';

export const textAlignVariants = styleVariants({
start: {
textAlign: 'start',
},
end: {
textAlign: 'end',
},
center: {
textAlign: 'center',
},
});

export const marginVariants = styleVariants(genericVars.space, (space) => [
{
margin: space,
Expand Down Expand Up @@ -45,3 +59,24 @@ export const paddingInlineVariants = styleVariants(
},
],
);

export type TextOverflow = 'ellipsis' | 'clip';

const textOverflowCssProps: Record<TextOverflow, StyleRule> = {
ellipsis: {
textOverflow: 'ellipsis',
},
clip: {
textOverflow: 'clip',
},
};

const textOverflowBase = style({
overflow: 'hidden',
whiteSpace: 'nowrap',
});

export const textOverflowVariants = styleVariants(
textOverflowCssProps,
(value) => [textOverflowBase, value],
);
13 changes: 12 additions & 1 deletion lib/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
paddingBlockVariants,
paddingInlineVariants,
paddingVariants,
TextAlign,
textAlignVariants,
TextOverflow,
textOverflowVariants,
} from './core.css.js';
import { Align, alignItems } from './layout.css.js';
import { Tooltip } from './tooltip.js';
Expand All @@ -32,6 +36,9 @@ export type BoxBasedComponentProps<T extends keyof ReactHTMLAttributesHacked> =
paddingBlock?: Space | undefined;
paddingInline?: Space | undefined;
tooltip?: ReactNode;
textAlign?: TextAlign | undefined;
textOverflow?: TextOverflow | undefined;

// rounded
}
>;
Expand All @@ -48,9 +55,11 @@ const BoxInner = <T extends keyof ReactHTMLAttributesHacked = 'div'>(
padding,
paddingBlock,
paddingInline,
textAlign,
textOverflow,
tooltip,
...props
}: BoxBasedComponentProps<T>,
}: PropsWithChildren<BoxBasedComponentProps<T>>,
ref: React.ForwardedRef<ReactHTMLAttributesHacked[T]>,
) => {
const el = createElement(
Expand All @@ -66,6 +75,8 @@ const BoxInner = <T extends keyof ReactHTMLAttributesHacked = 'div'>(
padding && paddingVariants[padding],
paddingBlock && paddingBlockVariants[paddingBlock],
paddingInline && paddingInlineVariants[paddingInline],
textAlign && textAlignVariants[textAlign],
textOverflow && textOverflowVariants[textOverflow],
className,
) || undefined,
ref,
Expand Down
8 changes: 6 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export const App: FC = () => {
<Grid>
<Inline>
<FormInputRadioGroup name="color-scheme">
<Heading level="3">Color Scheme</Heading>
<Heading level="3" textOverflow="ellipsis">
Color Scheme
</Heading>
<FormInputRadio
// inline
label="auto"
Expand All @@ -63,7 +65,9 @@ export const App: FC = () => {
</Inline>
<Inline>
<FormInputRadioGroup name="contrast-scheme">
<Heading level="3">Contrast Scheme</Heading>
<Heading level="3" textOverflow="ellipsis">
Contrast Scheme
</Heading>
<FormInputRadio
// inline
label="auto"
Expand Down

0 comments on commit 09618cd

Please sign in to comment.