Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Searchbar into Smaller Components #237

Merged
merged 3 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/components/Searchbar/CondensedSearchbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import styled from '@emotion/styled';
import Icon from '@leafygreen-ui/icon';
import IconButton from '@leafygreen-ui/icon-button';
import { uiColors } from '@leafygreen-ui/palette';
import { theme } from '../../theme/docsTheme';

// Defining as a styled component allows us to use as a selector in ExpandButton
const ExpandMagnifyingGlass = styled(Icon)``;

const ExpandButton = styled(IconButton)`
background-color: #fff;
background-image: none;
border: none;
border-radius: ${theme.size.medium};
box-shadow: none;
height: ${theme.size.large};
position: absolute;
right: ${theme.size.small};
/* 32px button in a 36px container, 2px top gives equal spacing */
top: 2px;
width: ${theme.size.large};
z-index: 1;
:hover,
:focus {
background-color: #f7f9f8;
${ExpandMagnifyingGlass} {
color: ${uiColors.gray.dark3};
transition: color 150ms ease-in;
}
}
:before {
display: none;
}
:after {
display: none;
}
`;

const CondensedSearchbar = ({ onExpand }) => (
<ExpandButton aria-label="Open MongoDB Docs Search" onClick={onExpand}>
Copy link
Member

@sophstad sophstad Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for missing this the first time around, but why is the onClick handler attached here instead of to the ExpandMagnifyingGlass IconButton? Oops, was a bit scrambled. What I meant is: What is the purpose of including the ExpandMagnifyingGlass icon when we are using an IconButton?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the button wraps around the icon and this is where the click handler is meant to be bound. This actually also increases the radius where a user can click and something can happen as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! So with the IconButton we pass the Icon as a child. We use styled here because I use it as a selector when styling the ExpandButton. It is really just an Icon but it has to be a styled(Icon) to use the selector as such.

<ExpandMagnifyingGlass glyph="MagnifyingGlass" fill={uiColors.gray.base} />
</ExpandButton>
);

export default CondensedSearchbar;
106 changes: 106 additions & 0 deletions src/components/Searchbar/ExpandedSearchbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { useCallback, useMemo } from 'react';
import { css } from '@emotion/core';
import styled from '@emotion/styled';
import Icon from '@leafygreen-ui/icon';
import IconButton from '@leafygreen-ui/icon-button';
import { uiColors } from '@leafygreen-ui/palette';
import useScreenSize from '../../hooks/useScreenSize';
import { theme } from '../../theme/docsTheme';
import SearchTextInput from './SearchTextInput';

const CLOSE_BUTTON_SIZE = theme.size.medium;
const GO_BUTTON_COLOR = uiColors.green.light3;
const GO_BUTTON_SIZE = '20px';

const removeDefaultHoverEffects = css`
background-image: none;
border: none;
box-shadow: none;
:before {
display: none;
}
:after {
display: none;
}
`;

const CloseButton = styled(IconButton)`
background-color: #fff;
border-radius: ${CLOSE_BUTTON_SIZE};
height: ${CLOSE_BUTTON_SIZE};
position: absolute;
right: ${theme.size.small};
/* button is 24 px and entire container is 36px so 6px top gives equal spacing */
top: 6px;
width: ${CLOSE_BUTTON_SIZE};
z-index: 1;
${removeDefaultHoverEffects};
`;

const GoButton = styled(IconButton)`
background-color: ${GO_BUTTON_COLOR};
border-radius: ${GO_BUTTON_SIZE};
height: ${GO_BUTTON_SIZE};
padding: 0;
position: absolute;
right: ${theme.size.default};
/* button is 20 px and entire container is 36px so 8px top gives equal spacing */
top: ${theme.size.small};
width: ${GO_BUTTON_SIZE};
z-index: 1;
${removeDefaultHoverEffects};
`;

const GoIcon = styled(Icon)`
/* Icon box size is 16px, 3px gives equal width and height */
left: 3px;
top: 3px;
height: 10px;
position: absolute;
width: 10px;
`;

const MagnifyingGlass = styled(Icon)`
color: ${uiColors.gray.base};
left: ${theme.size.default};
position: absolute;
/* This icon is 16px tall in a 36px tall container, so 10px gives equal spacing */
top: 10px;
transition: color 150ms ease-in;
z-index: 1;
`;

const ExpandedSearchbar = ({ isFocused, onChange, onMobileClose, value }) => {
const { isMobile } = useScreenSize();
const isSearching = useMemo(() => !!value && isFocused, [isFocused, value]);
const shouldShowGoButton = useMemo(() => !!value && !isMobile, [isMobile, value]);

const onSearchChange = useCallback(
e => {
const searchTerm = e.target.value;
onChange(searchTerm);
},
[onChange]
);

return (
<>
<MagnifyingGlass glyph="MagnifyingGlass" />
<SearchTextInput isSearching={isSearching} onChange={onSearchChange} value={value} />
{shouldShowGoButton && (
<GoButton aria-label="Go" href="#">
<GoIcon glyph="ArrowRight" fill="#13AA52" />
</GoButton>
)}
{isMobile && (
<CloseButton aria-label="Close Search" onClick={onMobileClose}>
<Icon glyph="X" fill={uiColors.gray.base} />
</CloseButton>
)}
</>
);
};

// Export this icon to be used as a selector by a parent component
export { MagnifyingGlass };
export default ExpandedSearchbar;
92 changes: 92 additions & 0 deletions src/components/Searchbar/SearchTextInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import React from 'react';
import { css } from '@emotion/core';
import styled from '@emotion/styled';
import { uiColors } from '@leafygreen-ui/palette';
import TextInput from '@leafygreen-ui/text-input';
import { theme } from '../../theme/docsTheme';

const SEARCHBAR_HEIGHT_OFFSET = '5px';

const activeTextBarStyling = css`
background-color: #fff;
border: none;
color: ${uiColors.gray.dark3};
`;

const StyledTextInput = styled(TextInput)`
/* Curve the text input box and put padding around text for icons/buttons */
div > input {
border: none;
background-color: ${uiColors.gray.light3};
border-radius: ${theme.size.medium};
color: ${uiColors.gray.dark1};
/* 24 px for magnifying glass plus 16px margin */
padding-left: 40px;
padding-right: ${theme.size.large};
font-weight: 300;
letter-spacing: 0.5px;
transition: background-color 150ms ease-in;
::placeholder {
color: ${uiColors.gray.dark1};
}
@media ${theme.screenSize.upToSmall} {
border: none;
:hover,
:focus {
border: none;
box-shadow: none;
}
}
}

/* Remove blue border on focus */
div > div:last-child {
display: none;
}
> label {
display: none;
}

@media ${theme.screenSize.upToSmall} {
background-color: #fff;
padding-bottom: ${theme.size.tiny};
${({ isSearching }) => isSearching && `box-shadow: 0 2px 2px 0 rgba(231,238,236,0.2);`};
div > input {
/* Always have this element filled in for mobile */
${activeTextBarStyling}
/* Switching font size on mobile allows us to prevent iOS Safari from zooming in */
font-size: ${theme.fontSize.default};
padding-top: 2px;
}
/**
On mobile, there is some space above the searchbar that is uncovered (on
desktop this is taken care of by the navbar). Here we can block elements
below from peeking through with a pseudoelement to cover this top space
*/
:before {
background-color: #fff;
bottom: 100%;
content: '';
position: absolute;
top: -${SEARCHBAR_HEIGHT_OFFSET};
width: 100%;
}
}
`;

const SearchTextInput = ({ isSearching, onChange, value, ...props }) => (
<StyledTextInput
autoFocus
label="Search Docs"
isSearching={isSearching}
onChange={onChange}
placeholder="Search Documentation"
tabIndex="0"
value={value}
{...props}
/>
);

// Also export the styled component for styled selector use
export { activeTextBarStyling, StyledTextInput };
export default SearchTextInput;
Loading