Skip to content

Commit

Permalink
refactor: replace emotion valid props with own solution
Browse files Browse the repository at this point in the history
  • Loading branch information
eels committed Oct 27, 2021
1 parent 9beabb5 commit 811342c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@
"styled"
],
"dependencies": {
"@emotion/is-prop-valid": "^1.1.0",
"classcat": "^5.0.3",
"html-attributes": "^1.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"style-vendorizer": "^2.1.1"
"style-vendorizer": "^2.1.1",
"svg-attributes": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.15.8",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import generateDisplayName from '@src/utils/generateDisplayName';
import hash from '@src/utils/hash';
import insertDynamicCSSRule from '@src/utils/insertDynamicCSSRule';
import isType from '@src/utils/isType';
import isValidProp from '@emotion/is-prop-valid';
import isValidProp from '@src/utils/isValidProp';
import prefixCSSDeclaration from '@src/utils/prefixCSSDeclaration';
import { createElement, forwardRef } from 'react';
import type { ChicProps, ConstructOptions, ExtendableObject } from '@types';
Expand Down
18 changes: 18 additions & 0 deletions src/store/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import htmlAttributes from 'html-attributes';
import svgAttributes from 'svg-attributes';

const reactAttributes = {
autoFocus: true,
children: true,
dangerouslySetInnerHTML: true,
defaultChecked: true,
defaultValue: true,
innerHTML: true,
key: true,
ref: true,
suppressContentEditableWarning: true,
suppressHydrationWarning: true,
valueLink: true,
};

export const ATTRIBUTES = Object.assign({}, htmlAttributes, reactAttributes, svgAttributes);
8 changes: 8 additions & 0 deletions src/utils/isValidProp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ATTRIBUTES } from '@src/store/attributes';

export default function isValidProp(prop: string) {
const isDOMAttribute = !!ATTRIBUTES[prop];
const isEventDataAriaProp = /^(([Aa][Rr][Ii][Aa]|[Dd][Aa][Tt][Aa])-.+)|(on[A-Z])/.test(prop);

return isDOMAttribute || isEventDataAriaProp;
}
15 changes: 15 additions & 0 deletions test/utils/isValidProps.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import isValidProp from '@src/utils/isValidProp';

describe('utils/isValidProp', () => {
it('should return true for valid DOM/React props', () => {
const props = ['aria-label', 'data-key', 'className', 'children', 'onClick'];

props.forEach((prop) => expect(isValidProp(prop)).toBe(true));
});

it('should return false for invalid DOM/React props', () => {
const props = ['isPrimary', 'hasBorder', 'withTextColor'];

props.forEach((prop) => expect(isValidProp(prop)).toBe(false));
});
});
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
"strict": true,
"target": "esnext",
"typeRoots": ["./types", "./node_modules/@types"]
}
},

"include": ["src/**/*", "types/**/*"]
}
12 changes: 12 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import tags from './tags';
import type { ComponentType, ForwardRefExoticComponent, PropsWithoutRef } from 'react';

declare module 'html-attributes' {
const attributes: { [key: string]: string };

export default attributes;
}

declare module 'svg-attributes' {
const attributes: { [key: string]: string };

export default attributes;
}

export type ExtendableObject<T = any> = { [key: string]: T };

export type AttrProps = ExtendableObject;
Expand Down

0 comments on commit 811342c

Please sign in to comment.