Skip to content

Commit b763840

Browse files
committed
feat(platform): preview project name on top of DSN
also fixes a "copied" feedback z-index issue closes #13015
1 parent f33155e commit b763840

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/components/codeBlock/code-blocks.module.scss

+1
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,5 @@
152152
border: none;
153153
color: var(--white);
154154
transition: opacity 150ms;
155+
z-index: 10000;
155156
}

src/components/codeBlock/index.tsx

+28-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ export interface CodeBlockProps {
1414
title?: string;
1515
}
1616

17+
/**
18+
*
19+
* Copy `element`'s text children as long as long as they are not `.no-copy`
20+
*/
21+
function getCopiableText(element: HTMLDivElement) {
22+
let text = '';
23+
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, {
24+
acceptNode: function (node) {
25+
// Skip if parent has .no-copy class
26+
if (node.parentElement?.classList.contains('no-copy')) {
27+
return NodeFilter.FILTER_REJECT;
28+
}
29+
return NodeFilter.FILTER_ACCEPT;
30+
},
31+
});
32+
33+
let node: Node | null;
34+
// eslint-disable-next-line no-cond-assign
35+
while ((node = walker.nextNode())) {
36+
text += node.textContent;
37+
}
38+
39+
return text.trim();
40+
}
41+
1742
export function CodeBlock({filename, language, children}: CodeBlockProps) {
1843
const [showCopied, setShowCopied] = useState(false);
1944
const codeRef = useRef<HTMLDivElement>(null);
@@ -32,7 +57,9 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) {
3257
return;
3358
}
3459

35-
const code = cleanCodeSnippet(codeRef.current.innerText, {language});
60+
const code = cleanCodeSnippet(getCopiableText(codeRef.current), {
61+
language,
62+
});
3663

3764
try {
3865
await navigator.clipboard.writeText(code);

src/components/codeKeywords/keywordSelector.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Selections,
2525
} from './styles.css';
2626
import {dropdownPopperOptions} from './utils';
27+
import {relative} from 'node:path/posix';
2728

2829
type KeywordSelectorProps = {
2930
group: string;
@@ -137,6 +138,7 @@ export function KeywordSelector({keyword, group, index}: KeywordSelectorProps) {
137138
// correctly overlap during animations, but this must be removed
138139
// after so copy-paste correctly works.
139140
display: isAnimating ? 'inline-grid' : undefined,
141+
position: 'relative',
140142
}}
141143
>
142144
<AnimatePresence initial={false}>
@@ -148,6 +150,28 @@ export function KeywordSelector({keyword, group, index}: KeywordSelectorProps) {
148150
{currentSelection[keyword]}
149151
</Keyword>
150152
</AnimatePresence>
153+
{!isOpen && currentSelection?.title && (
154+
<div
155+
className='no-copy'
156+
style={{
157+
position: 'absolute',
158+
top: '-24px',
159+
left: '50%',
160+
transform: 'translateX(-50%)',
161+
fontSize: '12px',
162+
backgroundColor: '#333',
163+
color: '#fff',
164+
padding: '2px 6px',
165+
borderRadius: '3px',
166+
pointerEvents: 'none',
167+
whiteSpace: 'nowrap',
168+
opacity: 0.9,
169+
userSelect: 'none',
170+
}}
171+
>
172+
{currentSelection.title}
173+
</div>
174+
)}
151175
</span>
152176
</KeywordDropdown>
153177
{isMounted &&

0 commit comments

Comments
 (0)