/*
Copyright (c) 2018-2020 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {
createStyled,
withStyle as styletronWithStyle,
useStyletron as styletronUseStyletron,
withWrapper as styletronWithWrapper,
} from 'styletron-react';
import {driver, getInitialStyle} from 'styletron-standard';
import type {StyleObject} from 'styletron-standard';
import type {ThemeT} from './types.js';
import {ThemeContext} from './theme-provider.js';
const wrapper = StyledComponent => {
return React.forwardRef((props, ref) => (
{theme => }
));
};
/* eslint-disable flowtype/generic-spacing */
/* eslint-disable flowtype/no-weak-types */
export type StyletronComponent<
Props,
> = React.StatelessFunctionalComponent & {
__STYLETRON__: any,
};
type StyleFn = {
(string): StyletronComponent<{}>,
(string, StyleObject): StyletronComponent<{}>,
(
string,
({$theme: Theme} & Props) => StyleObject,
): StyletronComponent,
>(
Base,
StyleObject,
): StyletronComponent<$Diff, {className: any}>>,
, Props>(
Base,
({$theme: Theme} & Props) => StyleObject,
): StyletronComponent<
$Diff, {className: any}> & Props,
>,
};
type ExtractPropTypes = (StyletronComponent) => T;
type WithStyleFn = {
, Props>(
Base,
(Props & {$theme: Theme}) => StyleObject,
): StyletronComponent<$Call & Props>,
>(
Base,
StyleObject,
): StyletronComponent<$Call>,
};
/* eslint-enable flowtype/generic-spacing */
/* eslint-enable flowtype/no-weak-types */
export function createThemedStyled(): StyleFn {
return ((createStyled({
wrapper,
getInitialStyle,
driver,
// eslint-disable-next-line flowtype/no-weak-types
}): any): StyleFn);
}
export const styled = createThemedStyled();
export function createThemedWithStyle(): WithStyleFn {
// eslint-disable-next-line flowtype/no-weak-types
return ((styletronWithStyle: any): WithStyleFn);
}
export const withStyle = createThemedWithStyle();
type UseStyletronFn = () => [(StyleObject) => string, Theme];
export function createThemedUseStyletron(): UseStyletronFn {
return function() {
// eslint-disable-next-line flowtype/no-weak-types
const theme = ((React.useContext(ThemeContext): any): Theme);
const [css] = styletronUseStyletron();
return [css, theme];
};
}
export const useStyletron = createThemedUseStyletron();
export function withWrapper(
// eslint-disable-next-line flowtype/no-weak-types
StyledElement: StyletronComponent,
wrapperFn: (
// eslint-disable-next-line flowtype/no-weak-types
StyletronComponent,
// eslint-disable-next-line flowtype/no-weak-types
) => any => any,
) {
// eslint-disable-next-line flowtype/no-weak-types
return styletronWithWrapper, any>(
StyledElement,
Styled => {
return React.forwardRef((props, ref) => (
{theme => wrapperFn(Styled)({ref: ref, ...props, $theme: theme})}
));
},
);
}
declare var __DEV__: boolean;
declare var __NODE__: boolean;
declare var __BROWSER__: boolean;