Skip to content

Commit cff3390

Browse files
committed
handle mobile search
1 parent 747eb8c commit cff3390

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

scaladoc-js/main/src/searchbar/SearchbarComponent.scala

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
140140
}.isEmpty
141141
}
142142
if matching.nonEmpty then {
143-
resultsDiv.appendChild(createKindSeparator("Recently searched", "fas fa-clock"))
143+
resultsDiv.appendChild(createKindSeparator("Recently searched", "fas fa-clock re-icon"))
144144
matching.map(_.toHTML).foreach(resultsDiv.appendChild)
145145
}
146146
}
@@ -201,6 +201,19 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
201201

202202
icon
203203

204+
private val mobileSearch =
205+
val mobileSearch = document.getElementById("mobile-scaladoc-searchbar-input").asInstanceOf[html.Input]
206+
mobileSearch.onfocus = (event: Event) =>
207+
if (document.body.contains(rootDiv)) {
208+
// document.body.removeChild(rootDiv)
209+
}
210+
else {
211+
document.body.appendChild(rootDiv)
212+
inputElem.focus()
213+
}
214+
// open the search if the user hits the `s` key when not focused on a text input
215+
document.body.addEventListener("keydown", (e: KeyboardEvent) => handleGlobalKeyDown(e))
216+
204217
private val inputElem: html.Input =
205218
input(cls := "scaladoc-searchbar-input", `type` := "search", `placeholder`:= "Find anything").tap { element =>
206219
element.addEventListener("input", { e =>
@@ -223,9 +236,18 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
223236
private val rootHiddenClasses = "hidden"
224237
private val rootShowClasses = ""
225238

226-
private val rootDiv: html.Div =
227-
val element = div(id := "scaladoc-searchbar")(
239+
private def generateRootDiv(): html.Div =
240+
val cancelButton = span(cls := "scaladoc-searchbar-cancel-button body-small")("Cancel")
241+
cancelButton.onclick = (event: Event) =>
242+
document.body.removeChild(rootDiv)
243+
244+
val inputContainer = div(cls := "scaladoc-searchbar-input-container")(
228245
inputElem,
246+
cancelButton,
247+
)
248+
249+
val element = div(id := "scaladoc-searchbar")(
250+
inputContainer,
229251
resultsDiv
230252
).tap { elem =>
231253
elem.addEventListener("mousedown", (e: Event) => e.stopPropagation())
@@ -267,6 +289,8 @@ class SearchbarComponent(engine: SearchbarEngine, inkuireEngine: InkuireJSSearch
267289

268290
rootParent
269291

292+
private val rootDiv: html.Div = generateRootDiv()
293+
270294
private def handleArrowUp() = {
271295
val selectedElement = resultsDiv.querySelector("[selected]")
272296
if selectedElement != null then {

scaladoc/resources/dotty_res/styles/theme/layout/searchBar.css

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
}
2828
}
2929

30-
@media(max-width: 768px) {
31-
#searchbar-container {
32-
display: none;
33-
}
34-
}
35-
3630
.scaladoc-searchbar-input {
3731
width: 100%;
3832
background-color: var(--action-primary-background-default-solid);
@@ -63,6 +57,28 @@
6357
pointer-events: none;
6458
}
6559

60+
.scaladoc-searchbar-input-container {
61+
display: flex;
62+
align-items: center;
63+
}
64+
65+
.scaladoc-searchbar-cancel-button {
66+
display: none;
67+
color: var(--text-primary);
68+
margin-left: calc(2 * var(--base-spacing));
69+
cursor: pointer;
70+
}
71+
72+
.scaladoc-searchbar-cancel-button:hover {
73+
color: var(--text-secondary);
74+
}
75+
76+
@media(max-width: 768px) {
77+
.scaladoc-searchbar-cancel-button {
78+
display: inline;
79+
}
80+
}
81+
6682
.theme-dark .scaladoc-searchbar-input::-webkit-search-cancel-button {
6783
background: url("../../../images/icon-buttons/close/dark/default.svg")
6884
no-repeat 50% 50%;
@@ -86,6 +102,11 @@
86102
overflow: scroll;
87103
}
88104

105+
.scaladoc-searchbar-row .micon {
106+
position: relative;
107+
top: -2px;
108+
}
109+
89110
.searchbar-hints {
90111
color: var(--text-primary);
91112
display: flex;
@@ -190,6 +211,10 @@
190211
border-top: solid 1px var(--border-default);
191212
}
192213

214+
.scaladoc-searchbar-row[divider]:first-of-type {
215+
border: none;
216+
}
217+
193218
.scaladoc-searchbar-row a {
194219
color: var(--text-secondary);
195220
text-decoration: none;
@@ -245,3 +270,36 @@
245270
margin-top: calc(1 * var(--base-spacing));
246271
}
247272
}
273+
274+
/* mobile view */
275+
276+
@media(max-width: 768px) {
277+
#searchbar-container {
278+
background-color: var(--background-default);
279+
}
280+
281+
#scaladoc-searchbar-results {
282+
border: none;
283+
}
284+
285+
.scaladoc-searchbar-input-container {
286+
margin-left: calc(3 * var(--base-spacing));
287+
margin-right: calc(3 * var(--base-spacing));
288+
}
289+
290+
.scaladoc-searchbar-row[divider]:first-child {
291+
border: none;
292+
}
293+
294+
#searchbar-footer {
295+
display: none;
296+
}
297+
298+
#scaladoc-searchbar-results {
299+
max-height: 100%;
300+
}
301+
302+
#scaladoc-searchbar-results {
303+
margin-top: calc(3 * var(--base-spacing));
304+
}
305+
}

scaladoc/src/dotty/tools/scaladoc/renderers/HtmlRenderer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
6969

7070
head(
7171
meta(charset := "utf-8"),
72-
meta(util.HTML.name := "viewport", content := "width=device-width, initial-scale=1"),
72+
meta(util.HTML.name := "viewport", content := "width=device-width, initial-scale=1, maximum-scale=1"),
7373
title(page.link.name),
7474
canonicalUrl(absolutePath(page.link.dri)),
7575
link(
@@ -253,7 +253,7 @@ class HtmlRenderer(rootPackage: Member, members: Map[DRI, Member])(using ctx: Do
253253
button(id := "mobile-menu-close", cls := "icon-button close"),
254254
),
255255
div(cls := "mobile-menu-container body-medium")(
256-
input(id := "mobile-scaladoc-searchbar-input", cls := "scaladoc-searchbar-input", `type` := "search", `placeholder`:= "Search documentation"),
256+
input(id := "mobile-scaladoc-searchbar-input", cls := "scaladoc-searchbar-input", `type` := "search", `placeholder`:= "Find anything"),
257257
a(href := "https://www.scala-lang.org/download/", cls := "mobile-menu-item") (
258258
"Download",
259259
),

0 commit comments

Comments
 (0)