MCPcopy Index your code
hub / github.com/adobe/react-spectrum / ExpandableCode

Function ExpandableCode

packages/dev/s2-docs/src/ExpandableCode.tsx:59–111  ·  view source on GitHub ↗
({
  children,
  hasHighlightedLine
}: {
  children: ReactNode;
  hasHighlightedLine?: boolean;
})

Source from the content-addressed store, hash-verified

57}
58
59export function ExpandableCode({
60 children,
61 hasHighlightedLine
62}: {
63 children: ReactNode;
64 hasHighlightedLine?: boolean;
65}) {
66 let state = useState(false);
67 let ctx = useContext(ExpandableCodeContext);
68 let [isExpanded, setExpanded] = ctx || state;
69 let ref = useRef<HTMLDivElement | null>(null);
70 let mask: string | undefined;
71 let padding: string | undefined;
72 if (!isExpanded) {
73 if (hasHighlightedLine) {
74 // mask the top, bottom, and right sides
75 mask =
76 'linear-gradient(transparent, white 25% 50%, transparent), linear-gradient(to right, white 0% 85%, transparent)';
77 padding = '0px';
78 } else {
79 // only mask the bottom and right
80 mask =
81 'linear-gradient(white 0% 50%, transparent), linear-gradient(to right, white 0% 85%, transparent)';
82 }
83 }
84
85 return (
86 <div ref={ref} className={example({isExpanded})}>
87 <div
88 style={
89 {
90 maskImage: mask,
91 maskComposite: 'intersect',
92 maxHeight: isExpanded ? undefined : 'inherit',
93 '--code-padding-y': padding
94 } as CSSProperties
95 }>
96 {children}
97 </div>
98 <div className={expandWrapper({isExpanded})}>
99 <ActionButton
100 onPress={() => {
101 viewTransition(() => {
102 setExpanded(!isExpanded);
103 ref.current?.scrollTo(0, 0);
104 });
105 }}>
106 {isExpanded ? 'Collapse code' : 'Expand code'}
107 </ActionButton>
108 </div>
109 </div>
110 );
111}
112
113function viewTransition(cb) {
114 if ('startViewTransition' in document) {

Callers

nothing calls this directly

Calls 1

viewTransitionFunction · 0.85

Tested by

no test coverage detected