Skip to content

Commit

Permalink
Makes search responsive on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
cbothner committed Nov 1, 2017
1 parent 00ce4f6 commit ecf3a36
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/javascript/catalog/CaseList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const UnstyledList = styled.ul`

const Image = styled.img.attrs({ role: 'presentation' })`
width: 50px;
min-width: 50px;
height: 50px;
border-radius: 2px;
margin-right: 1em;
Expand Down
34 changes: 31 additions & 3 deletions app/javascript/catalog/CatalogToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import * as React from 'react'
import styled, { css } from 'styled-components'
import { injectIntl } from 'react-intl'
import { withRouter } from 'react-router-dom'

Expand Down Expand Up @@ -39,11 +40,17 @@ const CatalogToolbar = ({ history }: ContextRouter) => (

export default withRouter(CatalogToolbar)

class SearchField extends React.Component<ContextRouter & { intl: IntlShape }> {
class SearchField extends React.Component<
ContextRouter & { intl: IntlShape },
{ active: boolean }
> {
state = { active: false }
input: ?HTMLInputElement

handleSubmit = (e: SyntheticEvent<*>) => {
e.preventDefault()
if (!this.input || this.input.value === '') return

this.input &&
this.props.history.push(
getSearchPath({
Expand All @@ -56,7 +63,10 @@ class SearchField extends React.Component<ContextRouter & { intl: IntlShape }> {

render () {
return (
<form onSubmit={this.handleSubmit}>
<FormCoveringToolbarOnMobile
active={this.state.active}
onSubmit={this.handleSubmit}
>
<InputGroup
inputRef={el => (this.input = el)}
className="pt-round"
Expand All @@ -71,10 +81,28 @@ class SearchField extends React.Component<ContextRouter & { intl: IntlShape }> {
id: 'catalog.search',
defaultMessage: 'Search cases',
})}
onFocus={() => this.setState({ active: true })}
onBlur={() => this.setState({ active: false })}
/>
</form>
</FormCoveringToolbarOnMobile>
)
}
}

const Search = withRouter(injectIntl(SearchField))

const FormCoveringToolbarOnMobile = styled.form`
@media screen and (max-width: 513px) {
background-color: #1d3f5e;
margin-left: -24px;
${({ active }) =>
active
? css`
margin-left: 0px;
position: absolute;
left: 14px;
width: calc(100vw - 28px);
`
: ''};
}
`
7 changes: 1 addition & 6 deletions app/javascript/catalog/Results.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import * as React from 'react'
import styled from 'styled-components'
import qs from 'qs'
import { map } from 'ramda'

Expand All @@ -16,6 +15,7 @@ import CaseList from 'catalog/CaseList'
import LibraryInfo from 'catalog/LibraryInfo'
import SearchForm from 'catalog/SearchForm'
import { Main, CatalogSection, SectionTitle } from 'catalog/shared'
import { Container as Sidebar } from 'catalog/Sidebar'

import type { ContextRouter } from 'react-router-dom'
import type { State } from 'catalog'
Expand Down Expand Up @@ -101,8 +101,3 @@ function getQueryFromPathname (pathname: string): { [string]: string[] } {
return params
}, {})
}

const Sidebar = styled.aside`
width: 18em;
margin: 0 0.5em 2em;
`
3 changes: 2 additions & 1 deletion app/javascript/catalog/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ const Sidebar = ({

export default Sidebar

const Container = styled.aside`
export const Container = styled.aside`
width: 18em;
@media (max-width: 700px) {
width: 100%;
}
Expand Down
9 changes: 7 additions & 2 deletions app/javascript/catalog/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Catalog extends React.Component<{ intl: IntlShape }, State> {
render () {
return (
<Router>
<div style={{ minHeight: '100%' }}>
<Container>
<CatalogToolbar />
<MaxWidthContainer>
<Window>
Expand Down Expand Up @@ -122,7 +122,7 @@ class Catalog extends React.Component<{ intl: IntlShape }, State> {
</Switch>
</Window>
</MaxWidthContainer>
</div>
</Container>
</Router>
)
}
Expand All @@ -135,6 +135,11 @@ class Catalog extends React.Component<{ intl: IntlShape }, State> {

export default injectIntl(Catalog)

const Container = styled.div`
min-height: 100%;
width: 100vw;
overflow: hidden;
`
const Window = styled.div`
min-height: 100%;
position: relative;
Expand Down

0 comments on commit ecf3a36

Please sign in to comment.