From 3cf0c3cd297b43c194d0d6820e13bcf8d8751fdc Mon Sep 17 00:00:00 2001 From: Nick Van Doorn Date: Sat, 6 Apr 2019 14:13:32 -0700 Subject: Init commit --- src/sunlight-theme.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/sunlight-theme.js (limited to 'src/sunlight-theme.js') diff --git a/src/sunlight-theme.js b/src/sunlight-theme.js new file mode 100644 index 0000000..e640003 --- /dev/null +++ b/src/sunlight-theme.js @@ -0,0 +1,21 @@ +import React from 'react' +import { useSunlight } from 'use-sunlight' +import { ThemeProvider } from 'emotion-theming' + +const isFn = t => typeof t === 'function' + +const pipeReducer = (a, b) => arg => { + if (!isFn(a) || !isFn(b)) throw new Error('Middleware has to be function') + return b(a(arg)) +} +const pipe = (...ops) => ops.reduce(pipeReducer) + +export const SunlightTheme = ({ children, middleware }) => { + const [lightLevel] = useSunlight() + const theme = { lightLevel } + // if we have at least one middleware function, + // them in a pipe (left to right), + const transformedTheme = + middleware && middleware.length ? pipe(...middleware)(theme) : theme + return {children} +} -- cgit v1.2.3