Skip to content

Commit

Permalink
Make popperConfig responsive
Browse files Browse the repository at this point in the history
- Search results appear more on-screen for narrow screens
- Refactor: replace magic literal (breakpoints to constants.js)
  • Loading branch information
kevinashworth committed Dec 28, 2020
1 parent d1e017d commit b8e046d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/octave/lib/components/common/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { registerComponent, Components, withAccess } from 'meteor/vulcan:core'
import React, { forwardRef, useState } from 'react'
import { useHistory } from 'react-router-dom'
import Media from 'react-media'
import { createBreakpointHook } from '@restart/hooks/useBreakpoint'
import Button from 'react-bootstrap/Button'
import Dropdown from 'react-bootstrap/Dropdown'
import FormControl from 'react-bootstrap/FormControl'
import InputGroup from 'react-bootstrap/InputGroup'
import algoliasearch from 'algoliasearch/lite'
import { Configure, connectHits, connectSearchBox, connectStateResults, Highlight, InstantSearch, PoweredBy, Snippet } from 'react-instantsearch-dom'
import { BREAKPOINTS } from '../../modules/constants'

// eslint-disable-next-line react/display-name
const CustomToggle = forwardRef(({ children }, ref) => (
Expand All @@ -16,13 +18,17 @@ const CustomToggle = forwardRef(({ children }, ref) => (
</span>
))

const popperConfig = {
modifiers: [{
name: 'offset',
options: {
offset: [0, 120]
}
}]
const popperConfigFn = (isMobile) => {
const skidding = 10
const distance = isMobile ? 10 : 100
return {
modifiers: [{
name: 'offset',
options: {
offset: [skidding, distance]
}
}]
}
}

const applicationid = Meteor.settings.public.algolia.ApplicationID
Expand All @@ -32,10 +38,14 @@ const searchClient = algoliasearch(applicationid, searchonlyapikey)

const Hits = ({ hits }) => {
const history = useHistory()
const useBreakpoint = createBreakpointHook({
mobile: BREAKPOINTS.MOBILE
})
const isMobile = useBreakpoint({ mobile: 'down' })
return (
<>
<Dropdown.Toggle as={CustomToggle} id='algolia-dropdown' />
<Dropdown.Menu popperConfig={popperConfig}>
<Dropdown.Menu popperConfig={popperConfigFn(isMobile)}>
<Dropdown.Header className='text-right'><PoweredBy /></Dropdown.Header>
{hits.length === 0
? <Dropdown.Item disabled>No search results</Dropdown.Item>
Expand Down Expand Up @@ -94,7 +104,7 @@ const Algolia = () => {
}
}
const toggle = () => setShow(!show)
const breakpoints = [555, 720, 885, 1215]
const breakpoints = BREAKPOINTS.ALGOLIA.VERTICAL
return (
<InstantSearch
indexName={algoliaindex}
Expand Down
7 changes: 7 additions & 0 deletions packages/octave/lib/modules/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,10 @@ export const BOOSTED = {
pastprojects: 0,
PASTPROJECTS: 0
}

export const BREAKPOINTS = {
ALGOLIA: {
VERTICAL: [555, 720, 885, 1215]
},
MOBILE: 576
}

0 comments on commit b8e046d

Please sign in to comment.