@primer/react
Version:
An implementation of GitHub's Primer Design System using React
111 lines (107 loc) • 5.13 kB
JavaScript
import React from 'react';
import { clsx } from 'clsx';
import styled, { createGlobalStyle } from 'styled-components';
import { TYPOGRAPHY, COMMON } from './constants.js';
import { useTheme } from './ThemeProvider.js';
import './FeatureFlags/FeatureFlags.js';
import { useFeatureFlag } from './FeatureFlags/useFeatureFlag.js';
import './FeatureFlags/DefaultFeatureFlags.js';
import Box from './Box/Box.js';
import { includesSystemProps, getTypographyAndCommonProps } from './utils/includeSystemProps.js';
import classes from './BaseStyles.module.css.js';
import 'focus-visible';
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_staff';
const GlobalStyle = createGlobalStyle(["*{box-sizing:border-box;}body{margin:0;}table{border-collapse:collapse;}input{color-scheme:", ";}[role=\"button\"]:focus:not(:focus-visible):not(.focus-visible),[role=\"tabpanel\"][tabindex=\"0\"]:focus:not(:focus-visible):not(.focus-visible),button:focus:not(:focus-visible):not(.focus-visible),summary:focus:not(:focus-visible):not(.focus-visible),a:focus:not(:focus-visible):not(.focus-visible){outline:none;box-shadow:none;}[tabindex=\"0\"]:focus:not(:focus-visible):not(.focus-visible),details-dialog:focus:not(:focus-visible):not(.focus-visible){outline:none;}"], props => props.colorScheme);
const StyledDiv = styled.div.withConfig({
displayName: "BaseStyles__StyledDiv",
componentId: "sc-nfjs56-0"
})(["", ";", ";"], TYPOGRAPHY, COMMON);
function BaseStyles(props) {
const {
children,
color,
fontFamily,
lineHeight,
className,
as: Component = 'div',
style,
...rest
} = props;
const {
colorScheme,
dayScheme,
nightScheme
} = useTheme();
const enabled = useFeatureFlag(CSS_MODULES_FEATURE_FLAG);
if (enabled) {
const newClassName = clsx(classes.BaseStyles, className);
const baseStyles = {
['--BaseStyles-fgColor']: color,
['--BaseStyles-fontFamily']: fontFamily,
['--BaseStyles-lineHeight']: lineHeight
};
// If props includes TYPOGRAPHY or COMMON props, pass them to the Box component
if (includesSystemProps(props)) {
const systemProps = getTypographyAndCommonProps(props);
return (
/*#__PURE__*/
// @ts-ignore shh
React.createElement(Box, _extends({
as: Component,
className: newClassName,
"data-portal-root": true
/**
* We need to map valid primer/react color modes onto valid color modes for primer/primitives
* valid color modes for primer/primitives: auto | light | dark
* valid color modes for primer/primer: auto | day | night | light | dark
*/,
"data-color-mode": colorScheme !== null && colorScheme !== undefined && colorScheme.includes('dark') ? 'dark' : 'light',
"data-light-theme": dayScheme,
"data-dark-theme": nightScheme,
style: {
...systemProps,
...baseStyles,
...style
}
}, rest), children)
);
}
return /*#__PURE__*/React.createElement(Component, _extends({
className: newClassName,
"data-portal-root": true
/**
* We need to map valid primer/react color modes onto valid color modes for primer/primitives
* valid color modes for primer/primitives: auto | light | dark
* valid color modes for primer/primer: auto | day | night | light | dark
*/,
"data-color-mode": colorScheme !== null && colorScheme !== undefined && colorScheme.includes('dark') ? 'dark' : 'light',
"data-light-theme": dayScheme,
"data-dark-theme": nightScheme,
style: {
...baseStyles,
...style
}
}, rest), children);
}
return /*#__PURE__*/React.createElement(StyledDiv, _extends({
className: className,
color: color !== null && color !== undefined ? color : 'var(--fgColor-default)',
fontFamily: fontFamily !== null && fontFamily !== undefined ? fontFamily : 'normal',
lineHeight: lineHeight !== null && lineHeight !== undefined ? lineHeight : 'default',
"data-portal-root": true
/**
* We need to map valid primer/react color modes onto valid color modes for primer/primitives
* valid color modes for primer/primitives: auto | light | dark
* valid color modes for primer/primer: auto | day | night | light | dark
*/,
"data-color-mode": colorScheme !== null && colorScheme !== undefined && colorScheme.includes('dark') ? 'dark' : 'light',
"data-light-theme": dayScheme,
"data-dark-theme": nightScheme,
style: style
}, rest), /*#__PURE__*/React.createElement(GlobalStyle, {
colorScheme: colorScheme !== null && colorScheme !== undefined && colorScheme.includes('dark') ? 'dark' : 'light'
}), children);
}
BaseStyles.displayName = "BaseStyles";
export { BaseStyles as default };