Skip to content

Commit

Permalink
feat(caption): add high-density support
Browse files Browse the repository at this point in the history
  • Loading branch information
cf-remylenoir committed Jan 11, 2024
1 parent 950f89f commit 19fb16c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/components/typography/src/Caption/Caption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
ExpandProps,
} from '@contentful/f36-core';
import { Text } from '../Text';
import { useDensity } from '@contentful/f36-utils';

const CAPTION_DEFAULT_TAG = 'span';

Expand All @@ -34,11 +35,13 @@ function _Caption<E extends React.ElementType = typeof CAPTION_DEFAULT_TAG>(
}: CaptionProps<E>,
ref: React.Ref<any>,
) {
const density = useDensity();

return (
<Text
as={CAPTION_DEFAULT_TAG}
testId={testId}
fontSize="fontSizeS"
fontSize={density === 'high' ? 'fontSizeSHigh' : 'fontSizeS'}
lineHeight="lineHeightS"
fontColor="gray900"
fontWeight={fontWeight}
Expand Down
4 changes: 4 additions & 0 deletions packages/components/typography/src/Caption/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ We provide you with `as` property, to allow you to set the tag of the `Caption`
## Props (API reference)

<PropsTable of="Caption" />

## Density support

This component supports multiple densities thanks to the [useDensity](/hooks/use-density) hook and automatically adjusts its styling for each density when wrapped with the [Density Container](/components/density-container).
35 changes: 35 additions & 0 deletions packages/components/typography/stories/Caption.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react';
import { Flex } from '@contentful/f36-core';
import type { Density } from '@contentful/f36-utils';
import { Heading } from '@contentful/f36-typography';

import { DensityContainer } from '../../density-container';
import { Caption, CaptionProps } from '../src/Caption/Caption';
import { Paragraph } from '../src/Paragraph/Paragraph';

Expand Down Expand Up @@ -38,3 +41,35 @@ export const Overview = (props: CaptionProps) => (
Overview.args = {
children: 'Caption',
};

export const WithDensitySupport = (props: CaptionProps) => {
const Densities = [
{
name: 'Low density',
density: 'low',
},
{
name: 'High density',
density: 'high',
},
];

return (
<Flex flexDirection="column" gap="spacingS">
{Densities.map((density) => {
return (
<Flex key={density.name} flexDirection="column">
<Heading>{density.name}</Heading>
<DensityContainer density={density.density as Density}>
<Caption {...props} />
</DensityContainer>
</Flex>
);
})}
</Flex>
);
};

WithDensitySupport.args = {
children: 'The brown fox jumps over the lazy dog',
};

0 comments on commit 19fb16c

Please sign in to comment.