Skip to content

✨ [bento][amp-sidebar] Preact version of amp-sidebar#31479

Merged
krdwan merged 9 commits intoampproject:masterfrom
krdwan:amp-sidebar-preact
Dec 11, 2020
Merged

✨ [bento][amp-sidebar] Preact version of amp-sidebar#31479
krdwan merged 9 commits intoampproject:masterfrom
krdwan:amp-sidebar-preact

Conversation

@krdwan
Copy link
Copy Markdown
Contributor

@krdwan krdwan commented Dec 7, 2020

First draft of Preact amp-sidebar component for initial review

12/11 - Update

  1. Added unit tests -
  • imperative API
  • click to open, close, toggle
  • click mask to close
  • left and right sides
  • animations
  1. Updated storybook
  • removed background content
  • add buttons to open, close, toggle to within sidebar
  1. Added type definition file

@krdwan krdwan requested a review from dvoytenko December 7, 2020 20:45
Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

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

This looks pretty much there. No significant comments on structure.

@krdwan krdwan requested a review from dvoytenko December 7, 2020 22:58
Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

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

Looks good, but one thing is not quite clear.

@krdwan krdwan marked this pull request as ready for review December 11, 2020 17:37
@caroqliu caroqliu self-requested a review December 11, 2020 17:42
Copy link
Copy Markdown
Contributor

@caroqliu caroqliu left a comment

Choose a reason for hiding this comment

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

Left a couple comments/questions but LGTM for the foundation. I'm curious what the plan is for:

  • checking/warning if the component is a direct descendent of <body>
  • responsive toolbar feature
  • future functionality for nested menus that interop with sidebar
  • bonus: if we were to add support for display locking, what would that look like? we could no longer conditionally render the markup

Would also be nice for the storybook to have a scrollable sample too.

* @return {{current: T}}
* @template T
*/
function useValueRef(current) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think lightbox also defines useValueRef, do you want to move it to preact/index or preact/utils instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

At this very initial stage, I'd like to keep it separate as I think a lot may change during the rest of the sidebar implementation. But I've added an item to the tracker to #31366 to look at shared items between this and lightbox. We have also talked about creating a common component that does the animations.

// To open, we mount and render the contents (invisible), then animate the display (visible).
// To close, it's the reverse.
// `mounted` mounts the component. `opened` plays the animation.
const initialOpen = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this is always false, do we need a named constant for the two times it's used?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure updated!

}, [onBeforeOpenRef]);
const close = useCallback(() => setOpened(false), []);
const toggle = useCallback(() => (opened ? close() : open()), [
opened,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it be helpful to have an openedRef so this callback value does not have to be redefined every time the state changes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, will address this as a follow up once we get further into the sidebar implementation.

// `mounted` mounts the component. `opened` plays the animation.
const initialOpen = false;
const [mounted, setMounted] = useState(initialOpen);
const [opened, setOpened] = useState(initialOpen);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: Should this follow [visible, setVisible] like lightbox? the meaning of "open" could get a bit overloaded here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I personally prefer opened since the sidebar docs all refer to the sidebar as being opened / closed. 🙂

: ANIMATION_KEYFRAMES_SLIDE_IN_RIGHT,
{
duration: ANIMATION_DURATION,
fill: 'both',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FMI: Why are some of these style objects given constant doc level names and others inlined here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is a tiny bit wonky, but my thought process was -

fill and direction are not configurable as they are fundamentally needed for the way the animation works.
duration and ease-in can be configured to make the animation slower / faster, curvier, and it still works, so thought it made sense to make as a constant.

I'm not married to it either way so let me know if you feel strongly about it.

side === 'left' ? classes.left : classes.right
}`}
role="menu"
tabindex="-1"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't a <div> not focusable by default?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, shouldn't it be focusable so it can listen for Esc to close?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The only ways to close that have been implemented so far are the imperative API and click on the mask. I have an action item in the tracker: #31366 for handling focus, so I'll follow up at that point :). This may be updated.

import {createUseStyles} from 'react-jss';

const sidebarClass = {
position: 'fixed !important',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

FMI: Why do we need these !importants?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Mostly just so the user cannot over overwrite by accident (or on purpose). The way the sidebar animates in and is positioned on the viewport is all critically dependent on position: fixed.

return (
<>
<Sidebar ref={ref} {...props}>
<div style={{margin: 8}}>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this margin necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not needed per se, but I think it looks infinitely better with the margin. 😂

Are you opposed to it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is storybook only, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, this is just in the storybook.

@krdwan krdwan merged commit 32e092f into ampproject:master Dec 11, 2020
@krdwan krdwan changed the title ✨ [bento] Preact version of amp-sidebar ✨ [bento][amp-sidebar] Preact version of amp-sidebar Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants