@primer/react
Version:
An implementation of GitHub's Primer Design System using React
152 lines (149 loc) • 3.36 kB
JavaScript
import { c } from 'react-compiler-runtime';
import React, { useState, useEffect } from 'react';
import { clsx } from 'clsx';
import classes from './Details.module.css.js';
import { useMergedRefs } from '../internal/hooks/useMergedRefs.js';
import { BoxWithFallback } from '../internal/components/BoxWithFallback.js';
import { jsx, jsxs } from 'react/jsx-runtime';
const Root = /*#__PURE__*/React.forwardRef((t0, forwardRef) => {
const $ = c(16);
let children;
let className;
let rest;
if ($[0] !== t0) {
({
className,
children,
...rest
} = t0);
$[0] = t0;
$[1] = children;
$[2] = className;
$[3] = rest;
} else {
children = $[1];
className = $[2];
rest = $[3];
}
const detailsRef = React.useRef(null);
const ref = useMergedRefs(forwardRef, detailsRef);
const [hasSummary, setHasSummary] = useState(false);
let t1;
let t2;
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
t1 = () => {
const {
current: details
} = detailsRef;
if (!details) {
return;
}
const updateSummary = () => {
const summary = details.querySelector("summary:not([data-default-summary])");
setHasSummary(!!summary);
};
updateSummary();
const observer = new MutationObserver(() => {
updateSummary();
});
observer.observe(details, {
childList: true,
subtree: true
});
return () => {
observer.disconnect();
};
};
t2 = [];
$[4] = t1;
$[5] = t2;
} else {
t1 = $[4];
t2 = $[5];
}
useEffect(t1, t2);
let t3;
if ($[6] !== className) {
t3 = clsx(className, classes.Details);
$[6] = className;
$[7] = t3;
} else {
t3 = $[7];
}
let t4;
if ($[8] !== hasSummary) {
t4 = !hasSummary && /*#__PURE__*/jsx(Details.Summary, {
"data-default-summary": true,
children: "See Details"
});
$[8] = hasSummary;
$[9] = t4;
} else {
t4 = $[9];
}
let t5;
if ($[10] !== children || $[11] !== ref || $[12] !== rest || $[13] !== t3 || $[14] !== t4) {
t5 = /*#__PURE__*/jsxs(BoxWithFallback, {
as: "details",
className: t3,
...rest,
ref: ref,
children: [t4, children]
});
$[10] = children;
$[11] = ref;
$[12] = rest;
$[13] = t3;
$[14] = t4;
$[15] = t5;
} else {
t5 = $[15];
}
return t5;
});
Root.displayName = 'Details';
function Summary(t0) {
var _as;
const $ = c(9);
let as;
let children;
let props;
if ($[0] !== t0) {
({
as,
children,
...props
} = t0);
$[0] = t0;
$[1] = as;
$[2] = children;
$[3] = props;
} else {
as = $[1];
children = $[2];
props = $[3];
}
const Component = (_as = as) !== null && _as !== void 0 ? _as : "summary";
const t1 = Component === "summary" ? null : "summary";
let t2;
if ($[4] !== Component || $[5] !== children || $[6] !== props || $[7] !== t1) {
t2 = /*#__PURE__*/jsx(Component, {
as: t1,
...props,
children: children
});
$[4] = Component;
$[5] = children;
$[6] = props;
$[7] = t1;
$[8] = t2;
} else {
t2 = $[8];
}
return t2;
}
Summary.displayName = 'Summary';
const Details = Object.assign(Root, {
Summary
});
export { Summary, Details as default };