Skip to content

Commit b1faf3c

Browse files
committed
added modal popup for godbolt links
1 parent 6ad706f commit b1faf3c

File tree

7 files changed

+130
-11
lines changed

7 files changed

+130
-11
lines changed

docs/libs/beman.optional/index.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
SPDX-License-Identifier: 2.0 license with LLVM exceptions
55
-->
66

7-
87
<!-- markdownlint-disable -->
98
<img src="https://raw.githubusercontent.com/bemanproject/beman/refs/heads/main/images/logos/beman_logo-beman_library_production_ready_api_may_undergo_changes.png" width="5%" height="auto" /> ![CI Tests](https://github.com/bemanproject/optional/actions/workflows/ci.yml/badge.svg) [![Coverage](https://coveralls.io/repos/github/bemanproject/optional/badge.svg?branch=main)](https://coveralls.io/github/bemanproject/optional?branch=main)
109
<!-- markdownlint-enable -->
@@ -117,9 +116,9 @@ beman::optional::optional<Cat&> cat = api();
117116
```
118117
119118
Full code can be found in [./examples/optional_ref.cpp](https://github.com/bemanproject/optional/blob/main/examples/optional_ref.cpp). Build and run instructions in [./examples/README.md](https://github.com/bemanproject/optional/blob/main/examples/README.md). Or try it on Compiler Explorer:
120-
119+
<Godbolt url="
121120
https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGIAKwAbKSuADJ4DJgAcj4ARpjEAQCcpAAOqAqETgwe3r4BwemZjgLhkTEs8Yn%2BKXaYDtlCBEzEBLk%2BfkG2mPYlDE0tBGXRcQnJts2t7fldCpNDESOVYzUAlLaoXsTI7BwA9HsA1JiqrKn0CnuoqX1iAPrEmFQAdMipqYcAtABUn27W1h%2BnxMGgAggdDkJlAARAAan3COwYSk%2BAEksII8DQxodQakmMgEJhPmZnhpDgB1VEAFQAEodQqEAGoAWU%2BJx2N2yINBPPMAGYIshvFhDiZ%2BW54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AbE4znECqABJK2lDsvk%2BWtiGbKhvTHT5Pl2pQ7WAZBkE%2BKRdwqhfiHj44HadkBN8OZ5f2XvZr9vqQexV6OwuHsLqPQKRNWhJgWIqIGD8EVGTB4TwODrHuhwfwvA/AcC0KQVAnB/h1UsCmTY2w9QCh4KQAgmhUHrGbCASQ7VnhJH8Fwdq/h2GSCSJIQIXB/D8P0JwSQvAWABA0KQbBuD8EcF4AoEA4jqE4NQaQOAsAYCIBACceof4SDkEoC0YAChlCGB6EID8UFOCULQI7OgtpsjGMiLQMxkFJG8GsfJegiRDHMFSAoD8BBSDuLoAkKI%2BtOBuLdh4hIAB5P8ziLFKJCKoeooJiCGPCUk%2BoTR8DYN4PwQQIgxDsCkDIQQigVDqESboXqBgjAoGLDYcasQ5GwEyiACGdxoZiFIGBRI7l47NlWOsaGciOCfFTE5UwRCLCll4KgXpRosAtMUqQZ%2BmI2DUlQJ4ZZ6xnxbB2HoVMEQHGmPMdgyh1YRypEsco9BmCJE0LwZwe22iiA1VUO1QInxAiSEOP/Wy/SGCDNsoQqwlhvy4EIPmChqxeCKK0EM0gRImBYESCs%2Bh/hxHoOEaQURmKHmJOkbI%2BRVCaHrFURorRyAdFkAoBAAxRiTFOLOTcwJkTbF9BOcylxjygmeJQLU30gLmxspsZ40JbAMl8piXElliSqWpPSTIzJyBskRAyfk4QohxAlM1eUtQjzdDBEFfU6Z%2Bg8DNPgBANpHSum0B6WMYViKRmcHGSVSZDTZl4IWaZcC8B1hrMcBsrZtAdkbH2cUiYOSuXxPObwS5pcbloM4Pc1xTyOAvOpW8w4Hyvk/MrIKgFazgU8QaRC/A2aYVwrJesZFqLKDJo4DivF4i01EtsCS%2BFtDSD0K4BoLFnB%2BQiLEQSqRGSu2It7f2wRHAh2jrmeOmtDqbzZAYUAA
122-
121+
"/>
123122
## How to Build
124123
125124
### Compiler Support

docusaurus.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ const config: Config = {
8484
],
8585
],
8686

87+
clientModules: [
88+
require.resolve('./src/components/Godbolt.jsx'),
89+
],
90+
8791
themeConfig: {
8892
//TODO: Replace with your project's social card
8993
image: './img/beman_logo.png',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"prism-react-renderer": "^2.3.0",
2424
"react": "^19.0.0",
2525
"react-dom": "^19.0.0",
26+
"react-modal": "^3.16.3",
2627
"unist-util-visit": "^5.0.0"
2728
},
2829
"devDependencies": {

src/components/CodeBlockWithMetadata.jsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import CodeBlock from "@theme/CodeBlock";
3+
import Godbolt from "./Godbolt";
34

45
let uniqueID = (function () {
56
let id = 0;
@@ -60,14 +61,7 @@ export default function CodeBlockWithMetadata({ children, meta, language }) {
6061

6162
return (
6263
<div style={{ marginBottom: "1em" }}>
63-
<a
64-
href={`https://godbolt.org/clientstate/${clientStateB64}`}
65-
target="_blank"
66-
rel="noopener noreferrer"
67-
style={{ display: "inline-block", marginBottom: "0.5em", color: "#0b5ed7" }}
68-
>
69-
Try it on Compiler Explorer 🚀
70-
</a>
64+
<Godbolt url={`https://godbolt.org/clientstate/${clientStateB64}`} />
7165
<CodeBlock language={language}>{sourceCode}</CodeBlock>
7266
</div>
7367
);

src/components/Godbolt.jsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React, { useState } from 'react';
2+
import Modal from 'react-modal';
3+
4+
// Bind modal to app element for accessibility
5+
Modal.setAppElement('#__docusaurus');
6+
7+
export default function Godbolt({ url }) {
8+
const [isOpen, setIsOpen] = useState(false);
9+
10+
const getHTMLIframe = (url) => {
11+
if (!url.includes('godbolt.org')) {
12+
return '<p>Invalid Godbolt URL</p>';
13+
}
14+
const fullUrl = url.trim().includes('godbolt.org/#')
15+
? url.replace('godbolt.org/#', 'godbolt.org/e?hideEditorToolbars=false#')
16+
: url;
17+
return `<iframe src="${fullUrl}" style="margin:auto; width:100%; height:100%; display:block; overflow:hidden;" frameBorder="0"></iframe>`;
18+
};
19+
20+
const openModal = () => setIsOpen(true);
21+
const closeModal = () => setIsOpen(false);
22+
const iframeHTML = getHTMLIframe(url);
23+
24+
return (
25+
<div>
26+
<a
27+
href={url}
28+
onClick={(e) => {
29+
e.preventDefault();
30+
openModal();
31+
}}
32+
style={{
33+
fontSize: '16px',
34+
color: '#007bff',
35+
textDecoration: 'underline',
36+
cursor: 'pointer',
37+
zIndex: 1000,
38+
}}
39+
onMouseOver={(e) => (e.target.style.color = '#0056b3')}
40+
onMouseOut={(e) => (e.target.style.color = '#007bff')}
41+
>
42+
Try it on Compiler Explorer 🚀
43+
</a>
44+
<Modal
45+
isOpen={isOpen}
46+
onRequestClose={closeModal}
47+
style={{
48+
content: {
49+
top: '50%',
50+
left: '50%',
51+
right: 'auto',
52+
bottom: 'auto',
53+
marginRight: '-50%',
54+
transform: 'translate(-50%, -50%)',
55+
width: '90%',
56+
height: '90%',
57+
padding: 0,
58+
},
59+
overlay: { zIndex: 1001 },
60+
}}
61+
contentLabel="Godbolt Compiler Modal"
62+
>
63+
<div
64+
style={{ width: '100%', height: '100%' }}
65+
dangerouslySetInnerHTML={{ __html: iframeHTML }}
66+
/>
67+
<button
68+
onClick={closeModal}
69+
style={{
70+
position: 'absolute',
71+
top: '10px',
72+
right: '10px',
73+
padding: '5px 10px',
74+
backgroundColor: '#dc3545',
75+
color: 'white',
76+
border: 'none',
77+
borderRadius: '5px',
78+
cursor: 'pointer',
79+
}}
80+
>
81+
Close
82+
</button>
83+
</Modal>
84+
</div>
85+
);
86+
}

src/theme/MDXComponents.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import MDXComponents from '@theme-original/MDXComponents';
3+
import Godbolt from '../components/Godbolt.jsx';
4+
5+
export default {
6+
...MDXComponents,
7+
Godbolt,
8+
};

yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,6 +4184,11 @@ execa@^5.0.0:
41844184
signal-exit "^3.0.3"
41854185
strip-final-newline "^2.0.0"
41864186

4187+
exenv@^1.2.0:
4188+
version "1.2.2"
4189+
resolved "https://registry.yarnpkg.com/exenv/-/exenv-1.2.2.tgz#2ae78e85d9894158670b03d47bec1f03bd91bb9d"
4190+
integrity sha512-Z+ktTxTwv9ILfgKCk32OX3n/doe+OcLTRtqK9pcL+JsP3J1/VW8Uvl4ZjLlKqeW4rzK4oesDOGMEMRIZqtP4Iw==
4191+
41874192
express@^4.17.3:
41884193
version "4.21.2"
41894194
resolved "https://registry.yarnpkg.com/express/-/express-4.21.2.tgz#cf250e48362174ead6cea4a566abef0162c1ec32"
@@ -7468,6 +7473,11 @@ react-json-view-lite@^1.2.0:
74687473
resolved "https://registry.yarnpkg.com/react-json-view-lite/-/react-json-view-lite-1.5.0.tgz#377cc302821717ac79a1b6d099e1891df54c8662"
74697474
integrity sha512-nWqA1E4jKPklL2jvHWs6s+7Na0qNgw9HCP6xehdQJeg6nPBTFZgGwyko9Q0oj+jQWKTTVRS30u0toM5wiuL3iw==
74707475

7476+
react-lifecycles-compat@^3.0.0:
7477+
version "3.0.4"
7478+
resolved "https://registry.yarnpkg.com/react-lifecycles-compat/-/react-lifecycles-compat-3.0.4.tgz#4f1a273afdfc8f3488a8c516bfda78f872352362"
7479+
integrity sha512-fBASbA6LnOU9dOU2eW7aQ8xmYBSXUIWr+UmF9b1efZBazGNO+rcXT/icdKnYm2pTwcRylVUYwW7H1PHfLekVzA==
7480+
74717481
react-loadable-ssr-addon-v5-slorber@^1.0.1:
74727482
version "1.0.1"
74737483
resolved "https://registry.yarnpkg.com/react-loadable-ssr-addon-v5-slorber/-/react-loadable-ssr-addon-v5-slorber-1.0.1.tgz#2cdc91e8a744ffdf9e3556caabeb6e4278689883"
@@ -7482,6 +7492,16 @@ react-loadable-ssr-addon-v5-slorber@^1.0.1:
74827492
dependencies:
74837493
"@types/react" "*"
74847494

7495+
react-modal@^3.16.3:
7496+
version "3.16.3"
7497+
resolved "https://registry.yarnpkg.com/react-modal/-/react-modal-3.16.3.tgz#c412d41915782e3c261253435d01468e2439b11b"
7498+
integrity sha512-yCYRJB5YkeQDQlTt17WGAgFJ7jr2QYcWa1SHqZ3PluDmnKJ/7+tVU+E6uKyZ0nODaeEj+xCpK4LcSnKXLMC0Nw==
7499+
dependencies:
7500+
exenv "^1.2.0"
7501+
prop-types "^15.7.2"
7502+
react-lifecycles-compat "^3.0.0"
7503+
warning "^4.0.3"
7504+
74857505
react-router-config@^5.1.1:
74867506
version "5.1.1"
74877507
resolved "https://registry.yarnpkg.com/react-router-config/-/react-router-config-5.1.1.tgz#0f4263d1a80c6b2dc7b9c1902c9526478194a988"
@@ -8740,6 +8760,13 @@ vfile@^6.0.0, vfile@^6.0.1:
87408760
"@types/unist" "^3.0.0"
87418761
vfile-message "^4.0.0"
87428762

8763+
warning@^4.0.3:
8764+
version "4.0.3"
8765+
resolved "https://registry.yarnpkg.com/warning/-/warning-4.0.3.tgz#16e9e077eb8a86d6af7d64aa1e05fd85b4678ca3"
8766+
integrity sha512-rpJyN222KWIvHJ/F53XSZv0Zl/accqHR8et1kpaMTD/fLCRxtV8iX8czMzY7sVZupTI3zcUTg8eycS2kNF9l6w==
8767+
dependencies:
8768+
loose-envify "^1.0.0"
8769+
87438770
watchpack@^2.4.1:
87448771
version "2.4.2"
87458772
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-2.4.2.tgz#2feeaed67412e7c33184e5a79ca738fbd38564da"

0 commit comments

Comments
 (0)