Skip to content

Commit

Permalink
revert to ui-box
Browse files Browse the repository at this point in the history
  • Loading branch information
hennessyevan committed Jul 2, 2019
1 parent 364e91a commit c73e0c9
Show file tree
Hide file tree
Showing 40 changed files with 1,017 additions and 1,239 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "box",
"version": "1.2.0",
"version": "1.5.0",
"description": "Blazing Fast React UI Primitive",
"contributors": [],
"repository": "@parishconnect/box",
Expand Down
Empty file modified src/@types/@emotion/hash/index.d.ts
100755 → 100644
Empty file.
51 changes: 28 additions & 23 deletions src/box.tsx
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import React from "react";
import { Is, BoxProps, BoxComponent } from "./types/box-types";
import enhanceProps from "./enhance-props";
import React from 'react'
import PropTypes from 'prop-types'
import {Is, BoxProps, BoxComponent} from './types/box-types'
import {propTypes} from './enhancers'
import enhanceProps from './enhance-props'

type Options<T extends Is> = {
is: T;
};
is: T
}

function createComponent<T extends Is>({ is: defaultIs }: Options<T>) {
const Component: BoxComponent<T> = ({ is = defaultIs, innerRef, children, ...props }: BoxProps<T>) => {
// Convert the CSS props to class names (and inject the styles)
const { className, enhancedProps: parsedProps } = enhanceProps(props);
const {className, enhancedProps: parsedProps} = enhanceProps(props)

parsedProps.className = className;
parsedProps.className = className

if (innerRef) {
parsedProps.ref = innerRef;
parsedProps.ref = innerRef
}

return React.createElement(is, parsedProps, children);
};
(Component as any).displayName = "Box";
// (Component as any).propTypes = {
// ...propTypes,
// innerRef: PropTypes.func,
// is: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
// };
(Component as any).defaultProps = {
return React.createElement(is, parsedProps, children)
}

;(Component as any).displayName = 'Box'

;(Component as any).propTypes = {
...propTypes,
innerRef: PropTypes.func,
is: PropTypes.oneOfType([PropTypes.string, PropTypes.func])
}

;(Component as any).defaultProps = {
innerRef: null,
is: "div",
boxSizing: "border-box"
};
is: 'div',
boxSizing: 'border-box'
}

return Component;
return Component
}

const Box = createComponent({ is: "div" });
const Box = createComponent({ is: 'div' })

export default Box;
export default Box
4 changes: 2 additions & 2 deletions src/cache.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export function get(property: string, value: CacheValue) {
export function set(property: string, value: CacheValue | object, className: string) {
if (process.env.NODE_ENV !== "production") {
const valueType = typeof value;
if (valueType !== "boolean" && valueType !== "number" && valueType !== "string" && valueType !== "object") {
if (valueType !== "boolean" && valueType !== "number" && valueType !== "string") {
const encodedValue = JSON.stringify(value);
throw new TypeError(`parishconnect-box: invalid cache value “${encodedValue}”. Only booleans, numbers and strings are supported.`);
throw new TypeError(`♱ ui-box: invalid cache value “${encodedValue}”. Only booleans, numbers and strings are supported.`);
}
}

Expand Down
106 changes: 29 additions & 77 deletions src/enhance-props.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,104 +1,56 @@
import * as cache from "./cache";
import { propEnhancers, propValueTypes } from "./enhancers";
import expandAliases from "./expand-aliases";
import * as styles from "./styles";
import { Without } from "./types/box-types";
import { EnhancerProps } from "./types/enhancers";
import facepaint from "facepaint";
import valueToString from "./value-to-string";
import {propEnhancers} from './enhancers'
import expandAliases from './expand-aliases'
import * as cache from './cache'
import * as styles from './styles'
import {Without} from './types/box-types'
import {EnhancerProps} from './types/enhancers'

type PreservedProps = Without<React.ComponentProps<any>, keyof EnhancerProps>;
type PreservedProps = Without<React.ComponentProps<any>, keyof EnhancerProps>

interface EnhancedPropsResult {
className: string;
enhancedProps: PreservedProps;
className: string
enhancedProps: PreservedProps
}

const mq = facepaint(["@media(max-width: 1121px)", "@media(max-width: 921px)", "@media(max-width: 421px)"]);

/**
* Converts the CSS props to class names and inserts the styles.
*/
export default function enhanceProps(rawProps: EnhancerProps & React.ComponentPropsWithoutRef<any>): EnhancedPropsResult {
const propsMap = expandAliases(rawProps);

const preservedProps: PreservedProps = {};
let className = rawProps.className || "";
const propsMap = expandAliases(rawProps)
const preservedProps: PreservedProps = {}
let className = rawProps.className || ''

for (const [propName, propValue] of propsMap) {
const cachedClassName = cache.get(propName, propValue);
const cachedClassName = cache.get(propName, propValue)
if (cachedClassName) {
className = `${className} ${cachedClassName}`;
continue;
}

if (Array.isArray(propValue)) {
const mqProps = mq({ [propName]: propValue })[0];
const propertyInfo = propValueTypes[propName];
const sharedClassName = "♱" + propertyInfo.className + "_" + propValue.join("-");
let addedClassName = false;

Object.keys(mqProps).forEach(key => {
if (typeof mqProps[key] !== "object") {
const enhancer = propEnhancers[propName];
if (enhancer && (propValue === null || propValue === undefined || propValue === false)) {
return;
} else if (!enhancer) {
// Pass through native props. e.g: disabled, value, type
preservedProps[propName] = propValue;
return;
}
const newCss = enhancer(mqProps[key]);
// Allow enhancers to return null for invalid values
if (newCss) {
styles.add(newCss.styles);
cache.set(propName, propValue, `${sharedClassName} ${newCss.className}`);
className = `${className} ${sharedClassName} ${newCss.className}`;
addedClassName = true;
}
} else {
const ruleString = `${propertyInfo.cssName}:${valueToString(Object.values(mqProps[key])[0])}`;
const newCss = {
className: sharedClassName,
styles: `
${key} {
.${sharedClassName} { ${ruleString} }
}
`
};

if (!addedClassName) {
className = `${className} ${sharedClassName}`;
}

styles.add(newCss.styles);
}
});

continue;
className = `${className} ${cachedClassName}`
continue
}

const enhancer = propEnhancers[propName];
const enhancer = propEnhancers[propName]
// Skip false boolean enhancers. e.g: `clearfix={false}`
// Also allows omitting props via overriding with `null` (i.e: neutralising props)
if (enhancer && (propValue === null || propValue === undefined || propValue === false)) {
continue;
if (
enhancer &&
(propValue === null || propValue === undefined || propValue === false)
) {
continue
} else if (!enhancer) {
// Pass through native props. e.g: disabled, value, type
preservedProps[propName] = propValue;
continue;
preservedProps[propName] = propValue
continue
}

const newCss = enhancer(propValue);
const newCss = enhancer(propValue)
// Allow enhancers to return null for invalid values
if (newCss) {
styles.add(newCss.styles);
cache.set(propName, propValue, newCss.className);
className = `${className} ${newCss.className}`;
styles.add(newCss.styles)
cache.set(propName, propValue, newCss.className)
className = `${className} ${newCss.className}`
}
}

className = className.trim();
className = className.trim()

return { className, enhancedProps: preservedProps };
return {className, enhancedProps: preservedProps}
}
98 changes: 43 additions & 55 deletions src/enhancers/background.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from "prop-types";
import getCss from "../get-css";
import { PropValidators, PropTypesMapping, PropEnhancerValueType, PropAliases, PropEnhancers } from "../types/enhancers";
import PropTypes from 'prop-types'
import getCss from '../get-css'
import { PropValidators, PropTypesMapping, PropEnhancerValueType, PropAliases, PropEnhancers } from '../types/enhancers'

export const propTypes: PropTypesMapping = {
background: PropTypes.string,
Expand All @@ -12,73 +12,61 @@ export const propTypes: PropTypesMapping = {
backgroundPosition: PropTypes.string,
backgroundRepeat: PropTypes.string,
backgroundSize: PropTypes.string
};
}

export const propAliases: PropAliases = {};
export const propAliases: PropAliases = {}

export const propValidators: PropValidators = {};
export const propValidators: PropValidators = {}

const background = {
className: "bg",
cssName: "background",
jsName: "background",
className: 'bg',
cssName: 'background',
jsName: 'background',
isPrefixed: true,
complexValue: true
};
}
const backgroundColor = {
className: "bg-clr",
cssName: "background-color",
jsName: "backgroundColor"
};
className: 'bg-clr',
cssName: 'background-color',
jsName: 'backgroundColor'
}
const backgroundImage = {
className: "bg-img",
cssName: "background-image",
jsName: "backgroundImage",
className: 'bg-img',
cssName: 'background-image',
jsName: 'backgroundImage',
isPrefixed: true,
complexValue: true
};
}
const backgroundPosition = {
className: "bg-pos",
cssName: "background-position",
jsName: "backgroundPosition"
};
className: 'bg-pos',
cssName: 'background-position',
jsName: 'backgroundPosition'
}
const backgroundSize = {
className: "bg-siz",
cssName: "background-size",
jsName: "backgroundSize"
};
className: 'bg-siz',
cssName: 'background-size',
jsName: 'backgroundSize'
}
const backgroundOrigin = {
className: "bg-orgn",
cssName: "background-origin",
jsName: "backgroundOrigin"
};
className: 'bg-orgn',
cssName: 'background-origin',
jsName: 'backgroundOrigin'
}
const backgroundRepeat = {
className: "bg-rpt",
cssName: "background-repeat",
jsName: "backgroundRepeat"
};
className: 'bg-rpt',
cssName: 'background-repeat',
jsName: 'backgroundRepeat'
}
const backgroundClip = {
className: "bg-clp",
cssName: "background-clip",
jsName: "backgroundClip"
};
className: 'bg-clp',
cssName: 'background-clip',
jsName: 'backgroundClip'
}
const backgroundBlendMode = {
className: "bg-blnd-md",
cssName: "background-blend-mode",
jsName: "backgroundBlendMode"
};

export const propValueTypes = {
background,
backgroundColor,
backgroundImage,
backgroundPosition,
backgroundSize,
backgroundOrigin,
backgroundRepeat,
backgroundClip,
backgroundBlendMode
};
className: 'bg-blnd-md',
cssName: 'background-blend-mode',
jsName: 'backgroundBlendMode'
}

export const propEnhancers: PropEnhancers = {
background: (value: PropEnhancerValueType) => getCss(background, value),
Expand All @@ -90,4 +78,4 @@ export const propEnhancers: PropEnhancers = {
backgroundPosition: (value: PropEnhancerValueType) => getCss(backgroundPosition, value),
backgroundRepeat: (value: PropEnhancerValueType) => getCss(backgroundRepeat, value),
backgroundSize: (value: PropEnhancerValueType) => getCss(backgroundSize, value)
};
}
Loading

0 comments on commit c73e0c9

Please sign in to comment.