Skip to content

Commit dc925e5

Browse files
committed
Add modal functionality to display HTML/JavaScript code
1 parent 3889160 commit dc925e5

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

chapters/what-is-an-api.qmd

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@ execute:
55
format:
66
html:
77
code-fold: true
8+
include-in-header:
9+
- text:
10+
<script src="https://unpkg.com/micromodal/dist/micromodal.min.js"></script>
811
lightbox: true
912
---
1013

14+
```{=html}
15+
<script>
16+
document.addEventListener("DOMContentLoaded", function() {
17+
MicroModal.init({
18+
awaitOpenAnimation: true,
19+
awaitCloseAnimation: true
20+
});
21+
});
22+
</script>
23+
```
24+
1125
An API (Application Programming Interface) is a set of rules and protocols enabling software programs to communicate and share information. While the term is often associated with "web APIs," it encompasses a broader concept. For instance, an API might allow a library to share its collection data with a museum or enable a weather service to provide updates to a news organization. APIs operate based on predefined rules set by developers, specifying how data can be accessed and used. While some APIs are public, most are internal, facilitating communication between systems within an organization.
1226

1327
## Using an API
@@ -72,6 +86,11 @@ displayCat(bengalCatData);
7286

7387
You can even get a random cat image!
7488

89+
**<a href="#" data-micromodal-trigger="mymodal" tabindex="0" class="popup-link">
90+
<i class="bi bi-box-arrow-up-right"></i>
91+
Code
92+
</a>**
93+
7594
::: {#cat-container .cell-html}
7695
<script>
7796
window.getRandomCat = async function () {
@@ -142,3 +161,55 @@ In research, APIs bridge the gap between data and analysis by automating data co
142161
## In summary
143162
APIs are not just tools for developers—they hold immense potential for researchers, too. By providing structured, dynamic access to datasets, APIs enable scholars to automate data collection, access real-time information, and integrate diverse data sources into their workflows. Whether you’re studying digital humanities, analyzing climate data, or investigating social media trends, APIs allow you to retrieve exactly the data you need, at scale and with precision. Embracing APIs as part of your research toolkit opens doors to innovative methodologies and insights that might otherwise remain out of reach.
144163
:::
164+
165+
166+
:::::: {#mymodal .modal .micromodal-slide aria-hidden="true"}
167+
::::: {.modal__overlay tabindex="-1" data-micromodal-close="true"}
168+
:::: {.modal__container role="dialog" aria-modal="true" aria-labelledby="mymodal-title"}
169+
170+
<button class="modal__close" aria-label="Close modal" data-micromodal-close></button>
171+
172+
<header>
173+
## Code {#mymodal-title}
174+
</header>
175+
176+
::: {#modal-1-content}
177+
178+
<pre><code>
179+
&lt;script&gt;
180+
window.getRandomCat = async function () {
181+
182+
const response = await fetch("https://api.thecatapi.com/v1/images/search?size=med&mime_types=jpg&format=json&has_breeds=true&order=RANDOM&page=0&limit=1");
183+
if (!response.ok) throw new Error("Failed to fetch random cat.");
184+
const data = await response.json();
185+
186+
const cat = data[0];
187+
const cat_id = cat.id;
188+
189+
const cat_detailed_info = await fetch(`https://api.thecatapi.com/v1/images/${cat_id}`);
190+
const cat_detailed_info_json = await cat_detailed_info.json();
191+
192+
// Safely access items that might be undefined or empty
193+
const breed = cat_detailed_info_json.breeds && cat_detailed_info_json.breeds.length > 0 ? cat_detailed_info_json.breeds[0].name : "Unknown";
194+
const description = cat_detailed_info_json.breeds && cat_detailed_info_json.breeds.length > 0 ? cat_detailed_info_json.breeds[0].description : "Unknown";
195+
196+
document.getElementById("cat-image").innerHTML = `
197+
&lt;div class="card" style="max-width: 300px; margin: 15px auto;"&gt;
198+
&lt;img src="${cat.url}" class="card-img-top" alt="Random Cat"&gt;
199+
&lt;div class="card-body"&gt;
200+
&lt;h5 class="card-title"&gt;Breed: ${breed}&lt;/h5&gt;
201+
&lt;p class="card-text"&gt;${description}&lt;/p&gt;
202+
&lt;/div&gt;
203+
&lt;/div&gt;
204+
`;
205+
};
206+
&lt;/script&gt;
207+
&lt;button onclick="getRandomCat()" class="btn btn-primary" style="padding: 10px 20px; font-size: 16px; display: block; margin: 0 auto;"&gt;Click here to get a random cat :)&lt;/button&gt;
208+
&lt;div id="cat-image" style="margin-top: 15px;"&gt;&lt;/div&gt;
209+
</code></pre>
210+
211+
:::
212+
213+
::::
214+
:::::
215+
::::::

styles.css

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
justify-content: flex-end;
1313
}
1414

15+
#cat-container {
16+
margin-bottom: 3em;
17+
}
1518

1619
.carousel-container {
1720
position: relative;
@@ -45,4 +48,100 @@
4548
}
4649

4750
.carousel-button.prev { left: 10px; }
48-
.carousel-button.next { right: 10px; }
51+
.carousel-button.next { right: 10px; }
52+
53+
/* Micromodal author: James Goldie. https://jimjam-slam.github.io/quarto-experiments/ */
54+
/* micromodal css: essentials for toggling/animating the dialog
55+
(otherwise it just sits there on the page!)
56+
*/
57+
58+
.modal__overlay {
59+
position: fixed;
60+
top: 0;
61+
left: 0;
62+
right: 0;
63+
bottom: 0;
64+
background: rgba(0,0,0,0.6);
65+
display: flex;
66+
justify-content: center;
67+
align-items: center;
68+
}
69+
70+
.modal__container {
71+
background-color: #fff;
72+
padding: 30px;
73+
/* max-width: 500px; */
74+
width: 90%;
75+
max-height: 90vh;
76+
border-radius: 4px;
77+
overflow-y: auto;
78+
box-sizing: border-box;
79+
}
80+
81+
@keyframes mmfadeIn {
82+
from { opacity: 0; }
83+
to { opacity: 1; }
84+
}
85+
86+
@keyframes mmfadeOut {
87+
from { opacity: 1; }
88+
to { opacity: 0; }
89+
}
90+
91+
@keyframes mmslideIn {
92+
from { transform: translateY(15%); }
93+
to { transform: translateY(0); }
94+
}
95+
96+
@keyframes mmslideOut {
97+
from { transform: translateY(0); }
98+
to { transform: translateY(-10%); }
99+
}
100+
101+
.micromodal-slide {
102+
display: none;
103+
}
104+
105+
.micromodal-slide.is-open {
106+
display: block;
107+
}
108+
109+
.micromodal-slide[aria-hidden="false"] .modal__overlay {
110+
animation: mmfadeIn .3s cubic-bezier(0.0, 0.0, 0.2, 1);
111+
}
112+
113+
.micromodal-slide[aria-hidden="false"] .modal__container {
114+
animation: mmslideIn .3s cubic-bezier(0, 0, .2, 1);
115+
}
116+
117+
.micromodal-slide[aria-hidden="true"] .modal__overlay {
118+
animation: mmfadeOut .3s cubic-bezier(0.0, 0.0, 0.2, 1);
119+
}
120+
121+
.micromodal-slide[aria-hidden="true"] .modal__container {
122+
animation: mmslideOut .3s cubic-bezier(0, 0, .2, 1);
123+
}
124+
125+
.micromodal-slide .modal__container,
126+
.micromodal-slide .modal__overlay {
127+
will-change: transform;
128+
}
129+
130+
.modal__close {
131+
background:transparent;
132+
border: 0;
133+
float: right;
134+
}
135+
136+
.modal__close::before {
137+
content: "\2715";
138+
}
139+
140+
/* Aditional styles for popup */
141+
.popup-link {
142+
cursor: pointer;
143+
color: #6c757d;
144+
text-decoration: none;
145+
margin-bottom: 1em;
146+
display: inline-block;
147+
}

0 commit comments

Comments
 (0)