Skip to content

Commit

Permalink
feat: layout flex
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeldking committed May 12, 2023
1 parent 4c0f6a4 commit 995e88d
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/empty/graphics/EmptyDocuments.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React, { useId } from 'react';
import { css } from '@emotion/react';
import { StyleProps } from '../../types';
import { useStyleProps } from '../../utils';

export const EmptyDocuments = (props: StyleProps) => {
// Create a unique ID so that more than one gradient def can exist
// TODO - try to hoist the gradient defs to be global
const id = useId();
const linearGradientIdPrefix = `#${id}-linear-gradient`;
const { styleProps } = useStyleProps(props);
return (
<svg
xmlns="http://www.w3.org/2000/svg"
xmlnsXlink="http://www.w3.org/1999/xlink"
viewBox="0 0 140 164"
{...props}
{...(styleProps as any)}
css={css`
.cls-1 {
isolation: isolate;
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './switch';
export * from './empty';
export * from './contextualhelp';
export * from './counter';
export * from './layout';
export { theme, designationColors } from './theme';
// export interface Props extends HTMLAttributes<HTMLDivElement> {
// /** custom content, defaults to 'the snozzberries taste like snozzberries' */
Expand Down
105 changes: 105 additions & 0 deletions src/layout/Flex.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
StyleHandlers,
classNames,
passthroughStyle,
responsiveDimensionValue,
useDOMRef,
useStyleProps,
} from '../utils';
import { DOMProps, DOMRef, FlexStyleProps } from '../types';
import { filterDOMProps } from '@react-aria/utils';
import React, { forwardRef, ReactNode } from 'react';
import { css } from '@emotion/react';

export interface FlexProps extends DOMProps, FlexStyleProps {
/** Children of the flex container. */
children: ReactNode;
}

const flexCSS = css`
display: flex;
`;

const flexStyleProps: StyleHandlers = {
direction: ['flexDirection', passthroughStyle],
wrap: ['flexWrap', flexWrapValue],
justifyContent: ['justifyContent', flexAlignValue],
alignItems: ['alignItems', flexAlignValue],
alignContent: ['alignContent', flexAlignValue],
};

function Flex(props: FlexProps, ref: DOMRef<HTMLDivElement>) {
let { children, ...otherProps } = props;

let matchedBreakpoints = ['base'];
let { styleProps } = useStyleProps(otherProps);
let { styleProps: flexStyle } = useStyleProps(otherProps, flexStyleProps);
let domRef = useDOMRef(ref);

// If no gaps, or native support exists, then we only need to render a single div.
let style = {
...styleProps.style,
...flexStyle.style,
};

if (props.gap != null) {
style.gap = responsiveDimensionValue(props.gap, matchedBreakpoints);
}

if (props.columnGap != null) {
style.columnGap = responsiveDimensionValue(
props.columnGap,
matchedBreakpoints
);
}

if (props.rowGap != null) {
style.rowGap = responsiveDimensionValue(props.rowGap, matchedBreakpoints);
}

return (
<div
css={flexCSS}
{...filterDOMProps(otherProps)}
className={classNames('flex', styleProps.className)}
style={style}
ref={domRef}
>
{children}
</div>
);
}

/**
* Normalize 'start' and 'end' alignment values to 'flex-start' and 'flex-end'
* in flex containers for browser compatibility.
*/
function flexAlignValue(value) {
if (value === 'start') {
return 'flex-start';
}

if (value === 'end') {
return 'flex-end';
}

return value;
}

/**
* Takes a boolean and translates it to flex wrap or nowrap.
*/
function flexWrapValue(value: boolean | 'wrap' | 'nowrap') {
if (typeof value === 'boolean') {
return value ? 'wrap' : 'nowrap';
}

return value;
}

/**
* A layout container using flexbox. Provides Spectrum dimension values, and supports the gap
* property to define consistent spacing between items.
*/
const _Flex = forwardRef(Flex);
export { _Flex as Flex };
1 change: 1 addition & 0 deletions src/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Flex';
86 changes: 85 additions & 1 deletion src/provider/GlobalStyles.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
import { Global, css } from '@emotion/react';

/**
* Medium size root CSS variables
*/
export const mediumRootCSS = css`
:root {
--ac-global-dimension-scale-factor: 1;
--ac-global-dimension-size-0: 0px;
--ac-global-dimension-size-10: 1px;
--ac-global-dimension-size-25: 2px;
--ac-global-dimension-size-30: 2px;
--ac-global-dimension-size-40: 3px;
--ac-global-dimension-size-50: 4px;
--ac-global-dimension-size-65: 5px;
--ac-global-dimension-size-75: 6px;
--ac-global-dimension-size-85: 7px;
--ac-global-dimension-size-100: 8px;
--ac-global-dimension-size-115: 9px;
--ac-global-dimension-size-125: 10px;
--ac-global-dimension-size-130: 11px;
--ac-global-dimension-size-150: 12px;
--ac-global-dimension-size-160: 13px;
--ac-global-dimension-size-175: 14px;
--ac-global-dimension-size-185: 15px;
--ac-global-dimension-size-200: 16px;
--ac-global-dimension-size-225: 18px;
--ac-global-dimension-size-250: 20px;
--ac-global-dimension-size-275: 22px;
--ac-global-dimension-size-300: 24px;
--ac-global-dimension-size-325: 26px;
--ac-global-dimension-size-350: 28px;
--ac-global-dimension-size-400: 32px;
--ac-global-dimension-size-450: 36px;
--ac-global-dimension-size-500: 40px;
--ac-global-dimension-size-550: 44px;
--ac-global-dimension-size-600: 48px;
--ac-global-dimension-size-650: 52px;
--ac-global-dimension-size-675: 54px;
--ac-global-dimension-size-700: 56px;
--ac-global-dimension-size-750: 60px;
--ac-global-dimension-size-800: 64px;
--ac-global-dimension-size-900: 72px;
--ac-global-dimension-size-1000: 80px;
--ac-global-dimension-size-1125: 90px;
--ac-global-dimension-size-1200: 96px;
--ac-global-dimension-size-1250: 100px;
--ac-global-dimension-size-1600: 128px;
--ac-global-dimension-size-1700: 136px;
--ac-global-dimension-size-1800: 144px;
--ac-global-dimension-size-2000: 160px;
--ac-global-dimension-size-2400: 192px;
--ac-global-dimension-size-2500: 200px;
--ac-global-dimension-size-3000: 240px;
--ac-global-dimension-size-3400: 272px;
--ac-global-dimension-size-3600: 288px;
--ac-global-dimension-size-4600: 368px;
--ac-global-dimension-size-5000: 400px;
--ac-global-dimension-size-6000: 480px;
}
`;

export const globalCSS = css`
:root {
--ac-global-dimension-static-size-0: 0px;
Expand Down Expand Up @@ -85,9 +145,33 @@ export const globalCSS = css`

--ac-global-rounding-small: var(--ac-global-dimension-static-size-50);
--ac-global-rounding-medium: var(--ac-global-dimension-static-size-100);

--ac-alias-border-size-thin: var(--ac-global-dimension-static-size-10);
--ac-alias-border-size-thick: var(--ac-global-dimension-static-size-25);
--ac-alias-border-size-thicker: var(--ac-global-dimension-static-size-50);
--ac-alias-border-size-thickest: var(--ac-global-dimension-static-size-100);
--ac-alias-border-offset-thin: var(--ac-global-dimension-static-size-25);
--ac-alias-border-offset-thick: var(--ac-global-dimension-static-size-50);
--ac-alias-border-offset-thicker: var(
--ac-global-dimension-static-size-100
);
--ac-alias-border-offset-thickest: var(
--ac-global-dimension-static-size-200
);
--ac-alias-grid-baseline: var(--ac-global-dimension-static-size-100);
--ac-alias-grid-gutter-xsmall: var(--ac-global-dimension-static-size-200);
--ac-alias-grid-gutter-small: var(--ac-global-dimension-static-size-300);
--ac-alias-grid-gutter-medium: var(--ac-global-dimension-static-size-400);
--ac-alias-grid-gutter-large: var(--ac-global-dimension-static-size-500);
--ac-alias-grid-gutter-xlarge: var(--ac-global-dimension-static-size-600);
--ac-alias-grid-margin-xsmall: var(--ac-global-dimension-static-size-200);
--ac-alias-grid-margin-small: var(--ac-global-dimension-static-size-300);
--ac-alias-grid-margin-medium: var(--ac-global-dimension-static-size-400);
--ac-alias-grid-margin-large: var(--ac-global-dimension-static-size-500);
--ac-alias-grid-margin-xlarge: var(--ac-global-dimension-static-size-600);
}
`;

export function GlobalStyles() {
return <Global styles={globalCSS} />;
return <Global styles={css(globalCSS, mediumRootCSS)} />;
}
1 change: 1 addition & 0 deletions src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type DimensionValue =

export type BorderRadiusValue = 'small' | 'medium';
export type BorderColorValue = 'light' | 'dark';
export type BorderSizeValue = 'thin' | 'thick' | 'thicker' | 'thickest';
export type BackgroundColorValue = 'light' | 'dark';

export type ColorValue =
Expand Down
1 change: 1 addition & 0 deletions src/types/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Direction = 'ltr' | 'rtl';
95 changes: 95 additions & 0 deletions src/types/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ export interface StyleProps {
/** Sets the CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. Only use as a **last resort**. **/
className?: string;

/** The margin for all four sides of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin). */
margin?: Responsive<DimensionValue>;
/** The margin for the logical start side of the element, depending on layout direction. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-inline-start). */
marginStart?: Responsive<DimensionValue>;
/** The margin for the logical end side of an element, depending on layout direction. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-inline-end). */
marginEnd?: Responsive<DimensionValue>;
// /** The margin for the left side of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left). Consider using `marginStart` instead for RTL support. */
// marginLeft?: Responsive<DimensionValue>,
// /** The margin for the right side of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left). Consider using `marginEnd` instead for RTL support. */
// marginRight?: Responsive<DimensionValue>,
/** The margin for the top side of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-top). */
marginTop?: Responsive<DimensionValue>;
/** The margin for the bottom side of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin-bottom). */
marginBottom?: Responsive<DimensionValue>;
/** The margin for both the left and right sides of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin). */
marginX?: Responsive<DimensionValue>;
/** The margin for both the top and bottom sides of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/margin). */
marginY?: Responsive<DimensionValue>;

/** The width of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/width). */
width?: DimensionValue;
/** The height of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/height). */
Expand All @@ -21,6 +40,82 @@ export interface StyleProps {
maxWidth?: string | number;
/** The maximum height of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/max-height). */
maxHeight?: string | number;

/** When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/flex). */
flex?: Responsive<string | number | boolean>;
/** When used in a flex layout, specifies how the element will grow to fit the space available. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow). */
flexGrow?: Responsive<number>;
/** When used in a flex layout, specifies how the element will shrink to fit the space available. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-shrink). */
flexShrink?: Responsive<number>;
/** When used in a flex layout, specifies the initial main size of the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis). */
flexBasis?: Responsive<number | string>;
/** Specifies how the element is justified inside a flex or grid container. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self). */
justifySelf?: Responsive<
| 'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'left'
| 'right'
| 'stretch'
>; // ...
/** Overrides the `alignItems` property of a flex or grid container. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/align-self). */
alignSelf?: Responsive<
| 'auto'
| 'normal'
| 'start'
| 'end'
| 'center'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'stretch'
>;
/** The layout order for the element within a flex or grid container. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/order). */
order?: Responsive<number>;

/** When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-area). */
gridArea?: Responsive<string>;
/** When used in a grid layout, specifies the column the element should be placed in within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column). */
gridColumn?: Responsive<string>;
/** When used in a grid layout, specifies the row the element should be placed in within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row). */
gridRow?: Responsive<string>;
/** When used in a grid layout, specifies the starting column to span within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column-start). */
gridColumnStart?: Responsive<string>;
/** When used in a grid layout, specifies the ending column to span within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column-end). */
gridColumnEnd?: Responsive<string>;
/** When used in a grid layout, specifies the starting row to span within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-start). */
gridRowStart?: Responsive<string>;
/** When used in a grid layout, specifies the ending row to span within the grid. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row-end). */
gridRowEnd?: Responsive<string>;

/** Specifies how the element is positioned. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/position). */
position?: Responsive<
'static' | 'relative' | 'absolute' | 'fixed' | 'sticky'
>;
/** The stacking order for the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index). */
zIndex?: Responsive<number>;
/** The top position for the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/top). */
top?: Responsive<DimensionValue>;
/** The bottom position for the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/bottom). */
bottom?: Responsive<DimensionValue>;
/** The logical start position for the element, depending on layout direction. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/inset-inline-start). */
start?: Responsive<DimensionValue>;
/** The logical end position for the element, depending on layout direction. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/inset-inline-end). */
end?: Responsive<DimensionValue>;
/** The left position for the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/left). Consider using `start` instead for RTL support. */
left?: Responsive<DimensionValue>;
/** The right position for the element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/right). Consider using `start` instead for RTL support. */
right?: Responsive<DimensionValue>;

/** Hides the element. */
isHidden?: Responsive<boolean>;
}

// These support more properties than specific arize components
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './classNames';
export * from './useDOMRef';
export * from './getWrappedElement';
export * from './styleProps';
Loading

0 comments on commit 995e88d

Please sign in to comment.