Skip to content

Commit

Permalink
Revert to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hennessyevan committed Jul 2, 2019
1 parent 1836197 commit 364e91a
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 102 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@parishconnect/box",
"version": "1.1.0",
"name": "box",
"version": "1.2.0",
"description": "Blazing Fast React UI Primitive",
"contributors": [],
"repository": "github.com/hennessyevan/box",
"license": "CC-BY-NC-ND-4.0",
"repository": "@parishconnect/box",
"license": "PCLL",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
6 changes: 2 additions & 4 deletions src/box.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useContext } from "react";
import React from "react";
import { Is, BoxProps, BoxComponent } from "./types/box-types";
import enhanceProps from "./enhance-props";
import { MediaQueryContext } from "./index";

type Options<T extends Is> = {
is: T;
Expand All @@ -10,8 +9,7 @@ type Options<T extends Is> = {
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 mqContext = useContext(MediaQueryContext);
const { className, enhancedProps: parsedProps } = enhanceProps(props, mqContext);
const { className, enhancedProps: parsedProps } = enhanceProps(props);

parsedProps.className = className;

Expand Down
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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") {
if (valueType !== "boolean" && valueType !== "number" && valueType !== "string" && valueType !== "object") {
const encodedValue = JSON.stringify(value);
throw new TypeError(`parishconnect-box: invalid cache value “${encodedValue}”. Only booleans, numbers and strings are supported.`);
}
Expand Down
14 changes: 4 additions & 10 deletions src/enhance-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ interface EnhancedPropsResult {
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>, mqContext?: any): EnhancedPropsResult {
export default function enhanceProps(rawProps: EnhancerProps & React.ComponentPropsWithoutRef<any>): EnhancedPropsResult {
const propsMap = expandAliases(rawProps);

const mq = facepaint(mqContext);

const preservedProps: PreservedProps = {};
let className = rawProps.className || "";

Expand All @@ -40,11 +40,6 @@ export default function enhanceProps(rawProps: EnhancerProps & React.ComponentPr

Object.keys(mqProps).forEach(key => {
if (typeof mqProps[key] !== "object") {
const cachedClassName = cache.get(propName, mqProps[key]);
if (cachedClassName) {
className = `${className} ${cachedClassName}`;
return;
}
const enhancer = propEnhancers[propName];
if (enhancer && (propValue === null || propValue === undefined || propValue === false)) {
return;
Expand All @@ -57,8 +52,7 @@ export default function enhanceProps(rawProps: EnhancerProps & React.ComponentPr
// Allow enhancers to return null for invalid values
if (newCss) {
styles.add(newCss.styles);
cache.set(propName, mqProps[key], newCss.className);
cache.set(propName, propValue, `${newCss.className} ${sharedClassName}`);
cache.set(propName, propValue, `${sharedClassName} ${newCss.className}`);
className = `${className} ${sharedClassName} ${newCss.className}`;
addedClassName = true;
}
Expand Down
25 changes: 12 additions & 13 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as cache from "./cache";
import * as styles from "./styles";
import * as cache from './cache'
import * as styles from './styles'

export { default } from "./box";
export { default as splitProps } from "./utils/split-props";
export { default as splitBoxProps } from "./utils/split-box-props";
export { default as MediaQueryContext, MediaQueryConsumer, MediaQueryProvider } from "./utils/media-query-context";
export {default} from './box'
export {default as splitProps} from './utils/split-props'
export {default as splitBoxProps} from './utils/split-box-props'

export {
background,
Expand All @@ -26,20 +25,20 @@ export {
propNames,
propAliases,
propEnhancers
} from "./enhancers/index";
} from './enhancers/index'

export const hydrate = cache.hydrate;
export const hydrate = cache.hydrate

export function extractStyles() {
const output = {
cache: cache.entries(),
styles: styles.getAll()
};
clearStyles();
return output;
}
clearStyles()
return output
}

export function clearStyles() {
cache.clear();
styles.clear();
cache.clear()
styles.clear()
}
9 changes: 0 additions & 9 deletions src/utils/media-query-context.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/utils/mqContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createContext } from "react";
import facepaint from "facepaint";

const mqContext = createContext(facepaint(["@media(min-width: 420px)", "@media(min-width: 920px)", "@media(min-width: 1120px)"]));

export default mqContext;
115 changes: 58 additions & 57 deletions test/box.tsx
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
import test from "ava";
import React from "react";
import * as render from "react-test-renderer";
import { shallow } from "enzyme";
import * as sinon from "sinon";
import Box from "../src/box";
import * as styles from "../src/styles";
import { propNames } from "../src/enhancers";
import test from 'ava'
import React from 'react'
import * as render from 'react-test-renderer'
import {shallow} from 'enzyme'
import * as sinon from 'sinon'
import Box from '../src/box'
import * as styles from '../src/styles'
import allPropertiesComponent from '../tools/all-properties-component'
import {propNames} from '../src/enhancers'

test.afterEach.always(() => {
styles.clear();
});
styles.clear()
})

// test.serial('all properties', t => {
// const component = allPropertiesComponent()
// const tree = render.create(component).toJSON()
// t.snapshot(tree, 'DOM')
// t.snapshot(styles.getAll(), 'CSS')
// })
test.serial('all properties', t => {
const component = allPropertiesComponent()
const tree = render.create(component).toJSON()
t.snapshot(tree, 'DOM')
t.snapshot(styles.getAll(), 'CSS')
})

test.serial("all properties set to inherit", t => {
const properties: any = {};
test.serial('all properties set to inherit', t => {
const properties: any = {}
for (const name of propNames) {
properties[name] = "inherit";
properties[name] = 'inherit'
}

delete properties.clearfix; // Non-css property
const component = <Box {...properties} />;
shallow(component);
t.snapshot(styles.getAll(), "CSS");
});
delete properties.clearfix // Non-css property
const component = <Box {...properties} />
shallow(component)
t.snapshot(styles.getAll(), 'CSS')
})

test.serial("all properties set to initial", t => {
const properties: any = {};
test.serial('all properties set to initial', t => {
const properties: any = {}
for (const name of propNames) {
properties[name] = "initial";
properties[name] = 'initial'
}

delete properties.clearfix; // Non-css property
const component = <Box {...properties} />;
shallow(component);
t.snapshot(styles.getAll(), "CSS");
});
delete properties.clearfix // Non-css property
const component = <Box {...properties} />
shallow(component)
t.snapshot(styles.getAll(), 'CSS')
})

test("is prop allows changing the dom element type", t => {
const component = shallow(<Box is="h1" />);
t.true(component.is("h1"));
});
test('is prop allows changing the dom element type', t => {
const component = shallow(<Box is="h1" />)
t.true(component.is('h1'))
})

test("is prop allows changing the component type", t => {
test('is prop allows changing the component type', t => {
function TestComponent(props) {
return <h1 {...props} />;
return <h1 {...props} />
}

const component = shallow(<Box is={TestComponent} />);
t.true(component.is(TestComponent));
});
const component = shallow(<Box is={TestComponent} />)
t.true(component.is(TestComponent))
})

test("innerRef prop gets passed the ref", t => {
const node = { domNode: true };
const innerRef = sinon.spy();
test('innerRef prop gets passed the ref', t => {
const node = {domNode: true}
const innerRef = sinon.spy()
render.create(<Box innerRef={innerRef} />, {
createNodeMock() {
return node;
return node
}
});
t.true(innerRef.calledOnce);
t.is(innerRef.args[0][0], node);
});
})
t.true(innerRef.calledOnce)
t.is(innerRef.args[0][0], node)
})

test("renders children", t => {
test('renders children', t => {
const component = shallow(
<Box>
<h1>Hi</h1>
</Box>
);
t.true(component.contains(<h1>Hi</h1>));
});
)
t.true(component.contains(<h1>Hi</h1>))
})

test("maintains the original className", t => {
const component = shallow(<Box className="derp" margin="10px" />);
t.true(component.hasClass("derp"));
});
test('maintains the original className', t => {
const component = shallow(<Box className="derp" margin="10px" />)
t.true(component.hasClass('derp'))
})
Loading

0 comments on commit 364e91a

Please sign in to comment.