Skip to content

Commit e646c67

Browse files
authored
Merge pull request #3272 from replicatedhq/reformat-dropdown-header
Reformat "open in chatgpt" dropdown
2 parents 9bb24cc + 9f57411 commit e646c67

File tree

9 files changed

+149
-233
lines changed

9 files changed

+149
-233
lines changed

docs/intro.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
slug: /
33
pagination_next: null
4+
hide_table_of_contents: true
45
---
56

67
# Home

src/components/CopyMarkdown.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import React, { useState, useEffect, useCallback, useRef } from 'react';
1111
import styles from './CopyMarkdown.module.css';
12+
import clsx from 'clsx';
1213

1314
function CopyMarkdown() {
1415
const [isOpen, setIsOpen] = useState(false);
@@ -137,8 +138,13 @@ function CopyMarkdown() {
137138

138139
// Initialize on client side
139140
useEffect(() => {
140-
// Check if we have markdown content
141-
const hasMarkdownContent = !!document.querySelector('.theme-doc-markdown.markdown h1');
141+
// Check for content
142+
const hasH1 = document.querySelector('h1');
143+
const hasArticle = document.querySelector('article');
144+
const hasMainContent = document.querySelector('main');
145+
146+
// Set content flag if we have both an h1 and either an article or main element
147+
const hasMarkdownContent = !!hasH1 && (!!hasArticle || !!hasMainContent);
142148
setHasContent(hasMarkdownContent);
143149

144150
// Set up click outside handler
@@ -174,10 +180,10 @@ function CopyMarkdown() {
174180
}
175181

176182
return (
177-
<div className={styles.container}>
183+
<div className={clsx(styles.container, 'copy-markdown-container')}>
178184
<button
179185
ref={buttonRef}
180-
className={`${styles.button} ${isCopied ? styles.copied : ''}`}
186+
className={clsx(styles.button, isCopied && styles.copied)}
181187
onClick={!isCopied ? toggleDropdown : undefined}
182188
aria-expanded={isOpen}
183189
aria-haspopup="true"
@@ -212,7 +218,7 @@ function CopyMarkdown() {
212218
aria-orientation="vertical"
213219
>
214220
<ul className={styles.list}>
215-
<li className={styles.item}>
221+
<li className={styles.item}>
216222
<button
217223
className={styles.actionButton}
218224
onClick={openInChatGpt}

src/components/CopyMarkdown.module.css

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
.container {
22
position: relative;
3-
display: inline-block;
4-
margin-left: auto;
3+
display: flex;
4+
align-items: center;
5+
margin-left: 0;
6+
z-index: 10;
7+
flex-shrink: 0;
8+
}
9+
10+
/* Add global class for targeting from parent components */
11+
:global(.copy-markdown-container) {
12+
position: relative;
13+
display: flex;
14+
align-items: center;
515
z-index: 10;
16+
flex-shrink: 0;
617
}
718

819
.copyIcon {
@@ -70,7 +81,6 @@
7081
}
7182

7283
.dropdown {
73-
display: block;
7484
position: absolute;
7585
top: 100%;
7686
left: 0;
@@ -218,7 +228,9 @@ html[data-theme='dark'] .actionDescription {
218228

219229
@media (max-width: 768px) {
220230
.container {
221-
margin-top: 1rem;
231+
margin-top: 0;
222232
margin-left: 0;
233+
flex-direction: column;
234+
align-items: flex-start;
223235
}
224236
}

src/components/HomepageFeatures.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/components/HomepageFeatures.module.css

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/theme/DocItem/Content/index.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React, {type ReactNode} from 'react';
9+
import clsx from 'clsx';
10+
import {ThemeClassNames} from '@docusaurus/theme-common';
11+
import {useDoc} from '@docusaurus/plugin-content-docs/client';
12+
import Heading from '@theme/Heading';
13+
import MDXContent from '@theme/MDXContent';
14+
import type {Props} from '@theme/DocItem/Content';
15+
import CopyMarkdown from '../../../components/CopyMarkdown';
16+
import styles from './styles.module.css';
17+
18+
/**
19+
Title can be declared inside md content or declared through
20+
front matter and added manually. To make both cases consistent,
21+
the added title is added under the same div.markdown block
22+
See https://github.com/facebook/docusaurus/pull/4882#issuecomment-853021120
23+
24+
We render a "synthetic title" if:
25+
- user doesn't ask to hide it with front matter
26+
- the markdown content does not already contain a top-level h1 heading
27+
*/
28+
function useSyntheticTitle(): string | null {
29+
const {metadata, frontMatter, contentTitle} = useDoc();
30+
const shouldRender =
31+
!frontMatter.hide_title && typeof contentTitle === 'undefined';
32+
if (!shouldRender) {
33+
return null;
34+
}
35+
return metadata.title;
36+
}
37+
38+
export default function DocItemContent({children}: Props): ReactNode {
39+
const syntheticTitle = useSyntheticTitle();
40+
return (
41+
<div className={clsx(ThemeClassNames.docs.docMarkdown, 'markdown', styles.docItemContainer)}>
42+
<CopyMarkdown />
43+
{syntheticTitle && (
44+
<header>
45+
<Heading as="h1">{syntheticTitle}</Heading>
46+
</header>
47+
)}
48+
<MDXContent>{children}</MDXContent>
49+
</div>
50+
);
51+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
.docHeader {
2+
display: flex;
3+
align-items: flex-start;
4+
justify-content: space-between;
5+
margin-bottom: 1.5rem;
6+
flex-wrap: wrap;
7+
gap: 1rem;
8+
}
9+
10+
/* When there's no title, align the dropdown to the right */
11+
.docHeader:empty,
12+
.docHeader:has(> :only-child) {
13+
justify-content: flex-end;
14+
}
15+
16+
.docTitle {
17+
font-size: 2.5rem;
18+
margin-bottom: 0;
19+
margin-right: 0;
20+
flex: 1 1 auto;
21+
min-width: 0;
22+
overflow-wrap: normal;
23+
word-wrap: normal;
24+
word-break: keep-all;
25+
hyphens: none;
26+
max-width: calc(100% - 180px);
27+
line-height: 1.2;
28+
}
29+
30+
.docItemContainer {
31+
position: relative;
32+
}
33+
34+
/* Position the CopyMarkdown component relative to the first h1 */
35+
.docItemContainer :global(.copy-markdown-container) {
36+
position: absolute;
37+
top: 0.75rem; /* Approximate vertical center of h1 */
38+
right: 0;
39+
z-index: 10;
40+
}
41+
42+
/* Ensure h1 elements have enough right margin for the dropdown */
43+
.docItemContainer :global(h1:first-of-type) {
44+
margin-right: 200px;
45+
margin-bottom: 1.5rem;
46+
}
47+
48+
/* Responsive design for mobile */
49+
@media (max-width: 768px) {
50+
.docHeader {
51+
flex-direction: column;
52+
align-items: flex-start;
53+
}
54+
55+
.docTitle {
56+
max-width: 100%;
57+
margin-bottom: 1rem;
58+
}
59+
60+
.docItemContainer :global(.copy-markdown-container) {
61+
position: static;
62+
margin-top: 1rem;
63+
margin-bottom: 1rem;
64+
}
65+
66+
.docItemContainer :global(h1:first-of-type) {
67+
margin-right: 0;
68+
margin-bottom: 1rem;
69+
}
70+
}

src/theme/DocItem/Layout/index.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)