This repository was archived by the owner on Feb 18, 2025. It is now read-only.
forked from styled-components/styled-components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstructWithOptions.js
More file actions
46 lines (39 loc) · 1.5 KB
/
constructWithOptions.js
File metadata and controls
46 lines (39 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// @flow
import type { Interpolation, Target } from '../types'
export default (css: Function) => {
const constructWithOptions = (
componentConstructor: Function,
tag: Target,
options: Object = {},
) => {
if (typeof tag !== 'string' && typeof tag !== 'function') {
// $FlowInvalidInputTest
throw new Error(`Cannot create styled-component for component: ${tag}`)
}
/* This is callable directly as a template function */
const templateFunction = (strings: Array<string>, ...interpolations: Array<Interpolation>) => {
const cssWrapperStart = '.hybridUI & { '
const cssWrapperEnd = ' }'
// We wrap the provided css string with our own specificity class (.hybridUI)
const updatedStrings = [...strings]
updatedStrings[0] = `${cssWrapperStart}${updatedStrings[0]}`
updatedStrings[updatedStrings.length - 1] = `${updatedStrings[
updatedStrings.length - 1
]}${cssWrapperEnd}`
return componentConstructor(tag, options, css(updatedStrings, ...interpolations))
}
/* If config methods are called, wrap up a new template function and merge options */
templateFunction.withConfig = config =>
constructWithOptions(componentConstructor, tag, {
...options,
...config,
})
templateFunction.attrs = attrs =>
constructWithOptions(componentConstructor, tag, {
...options,
attrs: { ...(options.attrs || {}), ...attrs },
})
return templateFunction
}
return constructWithOptions
}