theming: Provides its own types (#39555)

This commit is contained in:
Alexander T
2019-11-06 09:06:51 -08:00
committed by Nathan Shively-Sanders
parent 961d142370
commit b484f60b10
5 changed files with 6 additions and 163 deletions
+6
View File
@@ -4644,6 +4644,12 @@
"sourceRepoURL": "https://github.com/terser-js/terser",
"asOfVersion": "3.12.0"
},
{
"libraryName": "theming",
"typingsPackageName": "theming",
"sourceRepoURL": "https://github.com/cssinjs/theming",
"asOfVersion": "2.0.0"
},
{
"libraryName": "text-clipper",
"typingsPackageName": "text-clipper",
-59
View File
@@ -1,59 +0,0 @@
// Type definitions for theming 1.3
// Project: https://github.com/cssinjs/theming
// Definitions by: Sebastian Silbermann <https://github.com/eps1lon>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import * as React from "react";
export type Channel = string;
export type Theme = object | ((outerTheme: object) => object);
export const channel: "__THEMING__";
/**
* Returns a component with an already passed Theme listening on the default channel
*/
export function withTheme<P>(
component: React.ComponentType<P & { theme: Theme }>
): React.ComponentType<Pick<P, Exclude<keyof P, "theme">>>;
export interface ThemeProviderProps {
theme: Theme;
children?: React.ReactElement;
}
/**
* Provide a theme to an entire react component listening on the default channel
*/
export class ThemeProvider extends React.Component<ThemeProviderProps> {}
export type SubscriptionId = number;
export interface Broadcast<T extends Theme> {
getState(): T;
subscribe(callback: () => void): SubscriptionId;
unsubscribe(id: SubscriptionId): void;
}
export type ContextWithTheme<C extends string> = Record<C, Broadcast<Theme>>;
export interface ThemeListener<C extends string> {
initial(context: ContextWithTheme<C>): Theme;
subscribe(
context: ContextWithTheme<C>,
callback: (theme: Theme) => void
): SubscriptionId;
unsubscribe(context: ContextWithTheme<C>, id: SubscriptionId): void;
contextTypes: React.ValidationMap<C>;
}
/**
* ThemeListener for the default channel
*/
export const themeListener: ThemeListener<typeof channel>;
export interface Theming<C extends string> {
channel: C;
withTheme: typeof withTheme;
ThemeProvider: ThemeProvider;
themeListener: ThemeListener<C>;
}
/**
* creates a new Theming on the given channel
*/
export function createTheming<C extends string>(customChannel?: C): Theming<C>;
-79
View File
@@ -1,79 +0,0 @@
import * as React from "react";
import {
channel,
ContextWithTheme,
Theme,
themeListener,
ThemeProvider,
withTheme
} from "theming";
// Typings currently accept non-plain-objects while they get rejected at runtime.
// There exists currently no typing for plain objects.
const runtimeErrorTheme: Theme = [];
const customTheme = {
color: {
primary: "red",
secondary: "blue"
}
};
type CustomTheme = typeof customTheme;
interface DemoBoxProps {
text: string;
theme: CustomTheme;
}
const DemoBox = ({ text, theme }: DemoBoxProps) => {
return <div style={{ color: theme.color.primary }}>{text}</div>;
};
const ThemedDemoBox = withTheme(DemoBox);
const renderDemoBox = () => <ThemedDemoBox text="Hello, World!" />;
const App = () => {
return (
<ThemeProvider theme={customTheme}>
<ThemedDemoBox text="Theme provided" />
</ThemeProvider>
);
};
const AugmentedApp = () => {
return (
<ThemeProvider theme={customTheme}>
<ThemeProvider theme={outerTheme => ({ ...outerTheme, augmented: true })}>
<ThemedDemoBox text="Theme provided" />
</ThemeProvider>
</ThemeProvider>
);
};
function customWithTheme<P>(
// tslint:disable-next-line: no-unnecessary-generics
Component: React.ComponentType<P & { theme: object }>
) {
return class CustomWithTheme extends React.Component<P, { theme: object }> {
static contextTypes = themeListener.contextTypes;
setTheme = (theme: object) => this.setState({ theme });
subscription: number | undefined;
constructor(props: P, context: ContextWithTheme<typeof channel>) {
super(props, context);
this.state = { theme: themeListener.initial(context) };
}
componentDidMount() {
this.subscription = themeListener.subscribe(this.context, this.setTheme);
}
componentWillUnmount() {
const { subscription } = this;
if (subscription != null) {
themeListener.unsubscribe(this.context, subscription);
}
}
render() {
const { theme } = this.state;
return <Component theme={theme} {...this.props} />;
}
};
}
-24
View File
@@ -1,24 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"strictFunctionTypes": true
},
"files": [
"index.d.ts",
"theming-tests.tsx"
]
}
-1
View File
@@ -1 +0,0 @@
{ "extends": "dtslint/dt.json" }