Skip to content

Commit

Permalink
feat: layout component type docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SiTaggart committed Sep 19, 2023
1 parent e2d2d5c commit b3588aa
Show file tree
Hide file tree
Showing 47 changed files with 11,101 additions and 727 deletions.
6 changes: 6 additions & 0 deletions .changeset/fast-buses-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio-paste/media-object': patch
'@twilio-paste/core': patch
---

[Media Object]: Improved type annotations
6 changes: 6 additions & 0 deletions .changeset/grumpy-boxes-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio-paste/flex': patch
'@twilio-paste/core': patch
---

[Flex]: Improved TsDoc annotations
6 changes: 6 additions & 0 deletions .changeset/healthy-books-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio-paste/aspect-ratio': patch
'@twilio-paste/core': patch
---

[Aspect Ratio]: improved TsDoc annotations
6 changes: 6 additions & 0 deletions .changeset/neat-papayas-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio-paste/grid': patch
'@twilio-paste/core': patch
---

[Grid]: Improved TsDoc annotations
6 changes: 6 additions & 0 deletions .changeset/orange-flowers-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio-paste/codemods': patch
'@twilio-paste/core': patch
---

[Codemods] updated exports map
6 changes: 6 additions & 0 deletions .changeset/poor-ways-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio-paste/stack': patch
'@twilio-paste/core': patch
---

[Stack]: Improved TsDoc annotations
3 changes: 0 additions & 3 deletions packages/paste-codemods/tools/.cache/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@
"MediaFigure": "@twilio-paste/core/media-object",
"MediaObject": "@twilio-paste/core/media-object",
"Stack": "@twilio-paste/core/stack",
"getStackChildMargins": "@twilio-paste/core/stack",
"getStackDisplay": "@twilio-paste/core/stack",
"getStackStyles": "@twilio-paste/core/stack",
"BOX_PROPS_TO_BLOCK": "@twilio-paste/core/box",
"Box": "@twilio-paste/core/box",
"StyledBox": "@twilio-paste/core/box",
Expand Down
3 changes: 0 additions & 3 deletions packages/paste-codemods/tools/__tests__/tools.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ describe('generatePackageExportsMap', () => {
Stack: '@twilio-paste/core/stack',
StyledBox: '@twilio-paste/core/box',
getCustomElementStyles: '@twilio-paste/core/box',
getStackChildMargins: '@twilio-paste/core/stack',
getStackDisplay: '@twilio-paste/core/stack',
getStackStyles: '@twilio-paste/core/stack',
safelySpreadBoxProps: '@twilio-paste/core/box',
useComboboxPrimitive: '@twilio-paste/core/combobox-primitive',
useDisclosurePrimitiveState: '@twilio-paste/core/disclosure-primitive',
Expand Down
4 changes: 3 additions & 1 deletion packages/paste-core/layout/aspect-ratio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"dist"
],
"scripts": {
"build": "yarn clean && NODE_ENV=production node build.js && tsc",
"build": "yarn clean && NODE_ENV=production node build.js && tsc && yarn build:typedocs",
"build:js": "NODE_ENV=development node build.js",
"build:typedocs": "tsx ../../../../tools/build/generate-type-docs",
"clean": "rm -rf ./dist",
"tsc": "tsc"
},
Expand Down Expand Up @@ -54,6 +55,7 @@
"prop-types": "^15.7.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"tsx": "^3.12.10",
"typescript": "^4.9.4"
}
}
64 changes: 64 additions & 0 deletions packages/paste-core/layout/aspect-ratio/src/AspectRatio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react';
import {Box} from '@twilio-paste/box';
import {styled} from '@twilio-paste/styling-library';

export interface AspectRatioProps {
/**
* Determines the aspect ratio of the element. Use a colon separated number pattern (width:height).
*
* @type {string}
* @memberof AspectRatioProps
*/
ratio: string;
children: NonNullable<React.ReactNode>;
}

const RATIO_REGEX = /^(\d+:\d*)$/;

const isCorrectPattern = (ratio: string): boolean => RATIO_REGEX.test(ratio);

const handlePropValidation = ({ratio}: AspectRatioProps): void => {
const hasRatio = ratio != null && ratio !== '';

if (!hasRatio) {
throw new Error(`[Paste: AspectRatio] Missing 'ratio' prop.`);
}

if (!isCorrectPattern(ratio)) {
throw new Error(`[Paste: AspectRatio] 'ratio' is invalid. Use a colon separated number pattern (4:3).`);
}
};

const AspectRatioContainer = styled.div`
position: relative;
embed,
iframe,
object,
video {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
`;

const AspectRatio = React.forwardRef<HTMLDivElement, AspectRatioProps>((props, ref) => {
handlePropValidation(props);

const aspectArray = props.ratio.split(':').map(Number);
const aspectPercent = (aspectArray[1] / aspectArray[0]) * 100;

return (
<AspectRatioContainer ref={ref} style={{paddingBottom: `${aspectPercent}%`}}>
<Box position="absolute" top={0} right={0} bottom={0} left={0}>
{props.children}
</Box>
</AspectRatioContainer>
);
});

AspectRatio.displayName = 'AspectRatio';

export {AspectRatio};
68 changes: 2 additions & 66 deletions packages/paste-core/layout/aspect-ratio/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,66 +1,2 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import {Box} from '@twilio-paste/box';
import {styled} from '@twilio-paste/styling-library';

export interface AspectRatioProps {
ratio: string;
children: NonNullable<React.ReactNode>;
}

const RATIO_REGEX = /^(\d+:\d*)$/;

const isCorrectPattern = (ratio: string): boolean => RATIO_REGEX.test(ratio);

const handlePropValidation = ({ratio}: AspectRatioProps): void => {
const hasRatio = ratio != null && ratio !== '';

if (!hasRatio) {
throw new Error(`[Paste: AspectRatio] Missing 'ratio' prop.`);
}

if (!isCorrectPattern(ratio)) {
throw new Error(`[Paste: AspectRatio] 'ratio' is invalid. Use a colon separated number pattern (4:3).`);
}
};

const AspectRatioContainer = styled.div`
position: relative;
embed,
iframe,
object,
video {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
`;

const AspectRatio = React.forwardRef<HTMLDivElement, AspectRatioProps>((props, ref) => {
handlePropValidation(props);

const aspectArray = props.ratio.split(':').map(Number);
const aspectPercent = (aspectArray[1] / aspectArray[0]) * 100;

return (
<AspectRatioContainer ref={ref} style={{paddingBottom: `${aspectPercent}%`}}>
<Box position="absolute" top={0} right={0} bottom={0} left={0}>
{props.children}
</Box>
</AspectRatioContainer>
);
});

AspectRatio.displayName = 'AspectRatio';

if (process.env.NODE_ENV === 'development') {
AspectRatio.propTypes = {
children: PropTypes.node.isRequired,
ratio: PropTypes.string.isRequired,
};
}

export {AspectRatio};
export {AspectRatio} from './AspectRatio';
export type {AspectRatioProps} from './AspectRatio';
11 changes: 11 additions & 0 deletions packages/paste-core/layout/aspect-ratio/type-docs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"AspectRatio": {
"ratio": {
"type": "string",
"defaultValue": null,
"required": true,
"externalProp": false,
"description": "Determines the aspect ratio of the element. Use a colon separated number pattern (width:height)."
}
}
}
4 changes: 3 additions & 1 deletion packages/paste-core/layout/flex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"dist"
],
"scripts": {
"build": "yarn clean && NODE_ENV=production node build.js && tsc",
"build": "yarn clean && NODE_ENV=production node build.js && tsc && yarn build:typedocs",
"build:js": "NODE_ENV=development node build.js",
"build:typedocs": "tsx ../../../../tools/build/generate-type-docs",
"clean": "rm -rf ./dist",
"tsc": "tsc"
},
Expand Down Expand Up @@ -54,6 +55,7 @@
"prop-types": "^15.7.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"tsx": "^3.12.10",
"typescript": "^4.9.4"
}
}
131 changes: 131 additions & 0 deletions packages/paste-core/layout/flex/src/Flex.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import * as React from 'react';
import {Box, safelySpreadBoxProps} from '@twilio-paste/box';
import type {FlexboxProps} from '@twilio-paste/style-props';

import type {FlexProps} from './types';
import {getGrow, getShrink, getBasis, getVertical, getWrap, hAlignToProps, vAlignToProps} from './helpers';

const getFlexStyles = (props: FlexProps): FlexboxProps => {
const styles: FlexboxProps = {
justifyContent: props.vertical ? vAlignToProps(props) : hAlignToProps(props),
alignItems: props.vertical ? hAlignToProps(props) : vAlignToProps(props),
};

if (props.grow || props.shrink || props.basis) {
styles.flexGrow = getGrow(props);
styles.flexShrink = getShrink(props);
styles.flexBasis = getBasis(props);
}

if (props.vertical) {
styles.flexDirection = getVertical(props);
}

if (props.wrap) {
styles.flexWrap = getWrap(props);
}

return styles;
};

const Flex = React.forwardRef<HTMLDivElement, FlexProps>(
(
{
as,
basis,
children,
display,
element = 'FLEX',
hAlignContent,
grow,
marginTop,
marginRight,
marginBottom,
marginLeft,
margin,
marginX,
marginY,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
padding,
paddingX,
paddingY,
maxWidth,
minWidth = 'size0',
width,
height,
minHeight,
maxHeight,
size,
shrink,
vertical,
vAlignContent,
wrap,
...props
},
ref
) => {
const FlexStyles = React.useMemo(
() => getFlexStyles({basis, hAlignContent, grow, shrink, vertical, vAlignContent, wrap}),
[basis, hAlignContent, grow, shrink, vertical, vAlignContent, wrap]
);

if (size && (width || height)) {
// eslint-disable-next-line no-console
console.error('[Paste Flex]: you cannot set a height or width when using the size attribute');
}
if (
(marginX && (margin || marginBottom || marginLeft || marginRight || marginTop)) ||
(marginY && (margin || marginBottom || marginLeft || marginRight || marginTop))
) {
// eslint-disable-next-line no-console
console.error(
'[Paste Flex]: you cannot set a top, right, bottom or left margin when using the marginX or marginY attributes.'
);
}
if (
(paddingX && (padding || paddingBottom || paddingLeft || paddingRight || paddingTop)) ||
(paddingY && (padding || paddingBottom || paddingLeft || paddingRight || paddingTop))
) {
// eslint-disable-next-line no-console
console.error(
'[Paste Flex]: you cannot set a top, right, bottom or left padding when using the paddingX or paddingY attributes.'
);
}

const margins =
marginX || marginY ? {marginX, marginY} : {margin, marginBottom, marginLeft, marginRight, marginTop};
const paddings =
paddingX || paddingY ? {paddingX, paddingY} : {padding, paddingBottom, paddingLeft, paddingRight, paddingTop};
const widths = size ? {size} : {height, width};

return (
<Box
{...FlexStyles}
{...safelySpreadBoxProps(props)}
ref={ref}
as={as}
display={display}
element={element}
{...margins}
{...paddings}
minHeight={minHeight}
maxHeight={maxHeight}
maxWidth={maxWidth}
minWidth={minWidth}
{...widths}
>
{children}
</Box>
);
}
);

Flex.displayName = 'Flex';
Flex.defaultProps = {
display: 'flex',
};

export {Flex};
Loading

0 comments on commit b3588aa

Please sign in to comment.