@wonderflow/react-components
Version:
UI components from Wonderflow's Wanda design system
61 lines (60 loc) • 2.41 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/*
* Copyright 2022-2023 Wonderflow Design Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import tkns from '@wonderflow/tokens/platforms/web/tokens.json';
import { configResponsive, useResponsive as responsiveAhook } from 'ahooks';
import { createContext, useContext, useEffect, useState, } from 'react';
import { cssRelativeUnitsToPixel } from '../utils/conversions';
const DEFAULT_BREAKPOINTS = {
extraSmall: cssRelativeUnitsToPixel(tkns.breakpoint['extra-small']),
small: cssRelativeUnitsToPixel(tkns.breakpoint.small),
medium: cssRelativeUnitsToPixel(tkns.breakpoint.medium),
large: cssRelativeUnitsToPixel(tkns.breakpoint.large),
extraLarge: cssRelativeUnitsToPixel(tkns.breakpoint['extra-large']),
};
export const ResponsiveContext = createContext({
breakpoints: DEFAULT_BREAKPOINTS,
matches: {
extraSmall: false,
small: false,
medium: false,
large: false,
extraLarge: false,
},
});
ResponsiveContext.displayName = 'ResponsiveContext';
export const ResponsiveProvider = ({ children, breakpoints = DEFAULT_BREAKPOINTS, }) => {
configResponsive(breakpoints);
const responsiveHook = responsiveAhook();
const [matching, setMatching] = useState({
extraSmall: false,
small: false,
medium: false,
large: false,
extraLarge: false,
});
useEffect(() => {
setMatching(responsiveHook);
}, [breakpoints, responsiveHook]);
return (_jsx(ResponsiveContext.Provider, { value: { breakpoints, matches: matching }, children: children }));
};
export const useResponsiveContext = () => {
const context = useContext(ResponsiveContext);
if (!context) {
throw new Error('useResponsiveContext hook must be used inside ResponsiveProvider to access context data.');
}
return context;
};