Skip to content

Commit 260b147

Browse files
author
Andrey Okonetchnikov
committed
Convert mixins to TypeScript
1 parent aa7a5f6 commit 260b147

File tree

9 files changed

+2009
-1989
lines changed

9 files changed

+2009
-1989
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"react-dom": "^16.13.0",
5353
"react-styleguidist": "^11.0.0",
5454
"read-pkg-up": "^7.0.1",
55+
"typescript": "^4.3.2",
5556
"webpack": "^4.16.5"
5657
},
5758
"lint-staged": {

packages/mixins/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
"dependencies": {
88
"@component-driven/with-selector": "*",
99
"@theme-ui/color": "^0.3.1",
10-
"focus-visible": "^5.0.2"
10+
"focus-visible": "^5.0.2",
11+
"styled-system": "^5.1.5",
12+
"@styled-system/css": "^5.1.5"
1113
},
1214
"peerDependencies": {
1315
"react": ">=16.0",
1416
"react-dom": ">=16.0"
1517
},
18+
"devDependencies": {
19+
"@types/styled-components": "^5.1.3",
20+
"@types/styled-system": "^5.1.10",
21+
"@types/styled-system__css": "^5.0.13",
22+
"@types/theme-ui": "^0.6.0",
23+
"@types/theme-ui__color": "^0.3.2"
24+
},
1625
"author": "Andrey Okonetchnikov <andrey@okonet.ru>",
1726
"license": "MIT",
1827
"publishConfig": {

packages/mixins/src/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/mixins/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./mixins"

packages/mixins/src/mixins.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

packages/mixins/src/mixins.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { transparentize } from "@theme-ui/color"
2+
import { Theme } from "styled-system"
3+
import { CSSObject } from "@styled-system/css"
4+
5+
// Check if it's a props object or the theme
6+
function getTheme(theme: object = {}): object {
7+
// @ts-ignore theme-ui provides theme as an argument where styled-system uses props.theme
8+
return theme["theme"] || theme
9+
}
10+
/**
11+
* Mixin to generate consistent box-shadow rule for focus rings and selections
12+
*/
13+
export const focusBoxShadow =
14+
(color: string) =>
15+
(theme: Theme = {}): CSSObject => {
16+
return {
17+
boxShadow: `0 0 0 0.2em ${transparentize(color, 0.75)(getTheme(theme))}`
18+
}
19+
}
20+
21+
/**
22+
* Mixin to add an alternative focus ring to interactive elements.
23+
* It removes the default focus outline.
24+
*
25+
* @example
26+
* styled.div`
27+
* &:focus {
28+
* ${focusRingStyles('red')}
29+
* }
30+
* `
31+
*/
32+
export const focusRingStyles =
33+
(color: string, disabled: boolean = false) =>
34+
(theme: Theme) => {
35+
const themeColor = transparentize(color, 0)(getTheme(theme)) // This serves as a getter from theme
36+
if (disabled) {
37+
return {
38+
outline: "none"
39+
}
40+
}
41+
return {
42+
outline: "none",
43+
borderColor: themeColor,
44+
transition: "box-shadow .25s",
45+
...focusBoxShadow(themeColor)(theme)
46+
}
47+
}
48+
49+
/**
50+
* Mixin to add an alternative focus styles.
51+
* In addition to focusRingStyles mixin and adjust styles to use with focus-visible polyfill
52+
*
53+
* @example
54+
* styled.div`
55+
* ${focusRing('red')}
56+
* `
57+
*/
58+
export const focusRing =
59+
(color: string, disabled = false, hover = false) =>
60+
(theme: Theme) => {
61+
const styles = focusRingStyles(color, disabled)(getTheme(theme))
62+
const baseStyles = {
63+
".js-focus-visible &:focus:not(.focus-visible)": {
64+
outline: 0
65+
},
66+
"&.focus-visible": styles
67+
}
68+
if (hover) {
69+
return {
70+
...baseStyles,
71+
"&:hover:not(:disabled)": styles
72+
}
73+
}
74+
return baseStyles
75+
}
76+
77+
export const disabledStyles = {
78+
opacity: "0.6",
79+
filter: "saturate(60%)",
80+
cursor: "not-allowed"
81+
}
82+
83+
export function disabled() {
84+
return {
85+
"&:disabled": disabledStyles
86+
}
87+
}

styleguide.config.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,20 @@ module.exports = {
3333
Wrapper: file(".styleguidist/StyleguideProvider.tsx")
3434
},
3535
require: [file(".styleguidist/setup.js")],
36-
propsParser: require("react-docgen-typescript").withCustomConfig(
37-
file("packages/components/tsconfig.json"),
38-
{
39-
propFilter(prop) {
40-
if (prop.parent) {
41-
return (
42-
!prop.parent.fileName.includes("node_modules") ||
43-
prop.parent.fileName.includes("@types/styled-system")
44-
)
45-
}
46-
return true
47-
},
48-
componentNameResolver: (exp, source) =>
49-
exp.getName() === "StyledComponentClass" &&
50-
require("react-docgen-typescript").getDefaultExportForFile(source)
51-
}
52-
).parse,
36+
propsParser: require("react-docgen-typescript").withCustomConfig(file("tsconfig.json"), {
37+
propFilter(prop) {
38+
if (prop.parent) {
39+
return (
40+
!prop.parent.fileName.includes("node_modules") ||
41+
prop.parent.fileName.includes("@types/styled-system")
42+
)
43+
}
44+
return true
45+
},
46+
componentNameResolver: (exp, source) =>
47+
exp.getName() === "StyledComponentClass" &&
48+
require("react-docgen-typescript").getDefaultExportForFile(source)
49+
}).parse,
5350
webpackConfig: {
5451
module: {
5552
rules: [

packages/components/tsconfig.json renamed to tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
"strict": true,
1919
"target": "esnext"
2020
},
21-
"exclude": ["node_modules"],
21+
"exclude": ["packages/components/node_modules", "packages/mixins/node_modules"],
2222
"include": ["**/*.ts", "**/*.tsx"]
2323
}

0 commit comments

Comments
 (0)