UNPKG

2.24 kBJavaScriptView Raw
1"use client";
2
3import * as React from 'react';
4import { useContext, useMemo } from 'react';
5import { jsx as _jsx } from "react/jsx-runtime";
6export const DEFAULT_BREAKPOINTS = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
7export const DEFAULT_MIN_BREAKPOINT = 'xs';
8const ThemeContext = /*#__PURE__*/React.createContext({
9 prefixes: {},
10 breakpoints: DEFAULT_BREAKPOINTS,
11 minBreakpoint: DEFAULT_MIN_BREAKPOINT
12});
13const {
14 Consumer,
15 Provider
16} = ThemeContext;
17function ThemeProvider({
18 prefixes = {},
19 breakpoints = DEFAULT_BREAKPOINTS,
20 minBreakpoint = DEFAULT_MIN_BREAKPOINT,
21 dir,
22 children
23}) {
24 const contextValue = useMemo(() => ({
25 prefixes: {
26 ...prefixes
27 },
28 breakpoints,
29 minBreakpoint,
30 dir
31 }), [prefixes, breakpoints, minBreakpoint, dir]);
32 return /*#__PURE__*/_jsx(Provider, {
33 value: contextValue,
34 children: children
35 });
36}
37export function useBootstrapPrefix(prefix, defaultPrefix) {
38 const {
39 prefixes
40 } = useContext(ThemeContext);
41 return prefix || prefixes[defaultPrefix] || defaultPrefix;
42}
43export function useBootstrapBreakpoints() {
44 const {
45 breakpoints
46 } = useContext(ThemeContext);
47 return breakpoints;
48}
49export function useBootstrapMinBreakpoint() {
50 const {
51 minBreakpoint
52 } = useContext(ThemeContext);
53 return minBreakpoint;
54}
55export function useIsRTL() {
56 const {
57 dir
58 } = useContext(ThemeContext);
59 return dir === 'rtl';
60}
61function createBootstrapComponent(Component, opts) {
62 if (typeof opts === 'string') opts = {
63 prefix: opts
64 };
65 const isClassy = Component.prototype && Component.prototype.isReactComponent;
66 // If it's a functional component make sure we don't break it with a ref
67 const {
68 prefix,
69 forwardRefAs = isClassy ? 'ref' : 'innerRef'
70 } = opts;
71 const Wrapped = /*#__PURE__*/React.forwardRef(({
72 ...props
73 }, ref) => {
74 props[forwardRefAs] = ref;
75 const bsPrefix = useBootstrapPrefix(props.bsPrefix, prefix);
76 return /*#__PURE__*/_jsx(Component, {
77 ...props,
78 bsPrefix: bsPrefix
79 });
80 });
81 Wrapped.displayName = `Bootstrap(${Component.displayName || Component.name})`;
82 return Wrapped;
83}
84export { createBootstrapComponent, Consumer as ThemeConsumer };
85export default ThemeProvider;
\No newline at end of file