Skip to content

Commit

Permalink
feat(fuselage): Box rewritten in TypeScript (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan authored Mar 30, 2022
1 parent ce87027 commit acc8c85
Show file tree
Hide file tree
Showing 94 changed files with 739 additions and 693 deletions.
2 changes: 1 addition & 1 deletion packages/fuselage/src/.storybook/PropsVariation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ComponentType } from 'react';
import { Box } from '../components/Box';
import Box from '../components/Box';

function PropsVariation({
component: Component,
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage/src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ComponentProps, ReactElement, ReactNode } from 'react';
import React from 'react';

import { Box } from '../Box';
import Box from '../Box';
import { AccordionItem } from './AccordionItem';

type AccordionProps = ComponentProps<typeof Box> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useToggle, useUniqueId } from '@rocket.chat/fuselage-hooks';
import type { FormEvent, KeyboardEvent, MouseEvent, ReactNode } from 'react';
import React from 'react';

import { Box } from '../Box';
import Box from '../Box';
import { Chevron } from '../Chevron';
import { ToggleSwitch } from '../ToggleSwitch';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import React, { useEffect, useRef, useMemo, useState } from 'react';

import AnimatedVisibility from '../AnimatedVisibility';
import { Box } from '../Box';
import Box from '../Box';
import Chip from '../Chip';
import { Icon } from '../Icon';
import { InputBox } from '../InputBox';
Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage/src/components/Box/Box.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { css } from '@rocket.chat/css-in-js';
import { render } from '@testing-library/react';
import React from 'react';

import { Box } from '../..';
import Box from '.';

it('renders without crashing', () => {
render(<Box />);
Expand Down
66 changes: 66 additions & 0 deletions packages/fuselage/src/components/Box/Box.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type { cssFn } from '@rocket.chat/css-in-js';
import React, { createElement, forwardRef, memo } from 'react';
import type {
AllHTMLAttributes,
ElementType,
RefAttributes,
SVGAttributes,
Ref,
} from 'react';

import { useArrayLikeClassNameProp } from '../../hooks/useArrayLikeClassNameProp';
import { useBoxOnlyProps } from '../../hooks/useBoxOnlyProps';
import { useStyleSheet } from '../../hooks/useStyleSheet';
import type { Falsy } from '../../types/Falsy';
import { useBoxTransform, BoxTransforms } from './BoxTransforms';
import type { StylingProps } from './stylingProps';
import { useStylingProps } from './useStylingProps';

type BoxProps = {
is?: ElementType;
className?: string | cssFn | (string | cssFn | Falsy)[];
animated?: boolean;
withRichContent?: boolean | 'inlineWithoutBreaks';
htmlSize?: AllHTMLAttributes<HTMLElement>['size'];
} & Partial<StylingProps> &
Omit<
AllHTMLAttributes<HTMLElement>,
'ref' | 'is' | 'className' | 'size' | 'elevation'
> &
Omit<
SVGAttributes<SVGElement>,
keyof AllHTMLAttributes<HTMLElement> | 'elevation'
>;

export const Box = forwardRef(function Box(
{ is = 'div', children, ...props }: BoxProps,
ref: Ref<any>
) {
useStyleSheet();

const propsWithRef: BoxProps & RefAttributes<any> = props;

if (ref) {
propsWithRef.ref = ref;
}

let propsWithStringClassName = useArrayLikeClassNameProp(propsWithRef);

const transformFn = useBoxTransform();
if (transformFn) {
propsWithStringClassName = transformFn(propsWithStringClassName);
}

const propsWithoutStylingProps = useStylingProps(propsWithStringClassName);
const propsWithoutBoxOnlyProps = useBoxOnlyProps(propsWithoutStylingProps);

const element = createElement(is, propsWithoutBoxOnlyProps, children);

if (transformFn) {
return <BoxTransforms.Provider children={element} value={null} />;
}

return element;
});

export default memo(Box);
188 changes: 0 additions & 188 deletions packages/fuselage/src/components/Box/index.d.ts

This file was deleted.

37 changes: 0 additions & 37 deletions packages/fuselage/src/components/Box/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/fuselage/src/components/Box/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Box';
Loading

0 comments on commit acc8c85

Please sign in to comment.