Skip to content

Commit c4118b0

Browse files
committed
Improve docs search field - add clear button and keep it stickey
1 parent 7cc99a8 commit c4118b0

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

uilib-docs/src/components/navbar/index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React, {Component} from 'react';
22
import {StaticQuery, graphql} from 'gatsby';
3-
4-
import PropTypes from 'prop-types';
5-
import Link from 'gatsby-link';
3+
import classnames from 'classnames';
64
import _ from 'lodash';
75

86
import './index.scss';
97
import searchIcon from '../../images/search.svg';
8+
import clearIcon from '../../images/delete.svg';
109
import Item from './item';
1110

1211
class Navbar extends Component {
@@ -18,6 +17,10 @@ class Navbar extends Component {
1817
this.setState({filter: value});
1918
};
2019

20+
resetSearch = () => {
21+
this.setState({filter: ''});
22+
};
23+
2124
getCurrentPage = () => {
2225
if (typeof window !== 'undefined') {
2326
const path = window.location.href;
@@ -49,6 +52,26 @@ class Navbar extends Component {
4952
return filteredComponents;
5053
}
5154

55+
renderSearch = () => {
56+
const {filter} = this.state;
57+
const clearButtonClassName = classnames('clear-button', {
58+
hidden: _.isEmpty(filter)
59+
});
60+
return (
61+
<div className="search">
62+
<img src={searchIcon} />
63+
<input
64+
placeholder="Search..."
65+
onChange={this.setFilter}
66+
value={filter}
67+
/>
68+
<button className={clearButtonClassName} onClick={this.resetSearch}>
69+
<img src={clearIcon} />
70+
</button>
71+
</div>
72+
);
73+
};
74+
5275
renderNavbar = data => {
5376
const currentPage = this.getCurrentPage();
5477
const {filter} = this.state;
@@ -66,10 +89,7 @@ class Navbar extends Component {
6689

6790
return (
6891
<div className="navbar">
69-
<div className="search">
70-
<img src={searchIcon} />
71-
<input placeholder="Search..." onChange={this.setFilter} />
72-
</div>
92+
{this.renderSearch()}
7393
<ul>
7494
{_.map(markdowns, page => {
7595
return <Item id={page.title} link={page.path} />;

uilib-docs/src/components/navbar/index.scss

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
border-radius: 2px;
1212

1313
.search {
14+
position: sticky;
15+
top: 0;
16+
left: 0;
1417
display: flex;
1518
align-items: center;
1619
padding: 10px;
@@ -20,6 +23,8 @@
2023
width: 100%;
2124
transition: width 0.3s;
2225
border-radius: 2px;
26+
z-index: 10;
27+
overflow: hidden;
2328

2429
&:focus-within {
2530
width: 100%;
@@ -30,9 +35,32 @@
3035
height: 16px;
3136
}
3237

38+
.clear-button {
39+
border: 0;
40+
padding: 5px;
41+
background: transparent;
42+
background: $dark60;
43+
border-radius: 20px;
44+
display: flex;
45+
align-items: center;
46+
justify-content: center;
47+
cursor: pointer;
48+
transition: all 0.2s;
49+
50+
&.hidden {
51+
opacity: 0;
52+
transform: translateX(40px);
53+
}
54+
55+
> img {
56+
height: 14px;
57+
}
58+
}
59+
3360
input {
3461
background: transparent;
3562
color: $dark10;
63+
width: 100%;
3664
flex: 1;
3765
display: inline-block;
3866
margin-left: 10px;

uilib-docs/src/images/delete.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)