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

Feat: Add Mobile styles to FooterNav #181

Merged
merged 9 commits into from
May 22, 2020
Merged

Feat: Add Mobile styles to FooterNav #181

merged 9 commits into from
May 22, 2020

Conversation

haworku
Copy link
Contributor

@haworku haworku commented May 18, 2020

<FooterNav> and FooterExtendedNavList>

Completed

  • Add mobile styles and behavior
  • Refactor out new FooterExtendedNavList
  • Add tests

Testing

View Footer, FooterNav, FooterExtendedNavList components in mobile styles.


describe('isMobile', () => {
it.todo(
'renders mobile view when client window width is less than threshold'
Copy link
Contributor Author

@haworku haworku May 18, 2020

Choose a reason for hiding this comment

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

I can't figure out how to test window resize with running into typescript read-only limitations on reassigning window objects during a test. Ideally, I was going to use jest beforeAll to adjust the window size to mobile before running all the tests in this describe block.

Copy link
Contributor

Choose a reason for hiding this comment

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

could you use the resizeTo method instead of reassigning the dimensions? https://developer.mozilla.org/en-US/docs/Web/API/Window/resizeTo

imo this would be a great test to have!

Copy link
Contributor Author

@haworku haworku May 21, 2020

Choose a reason for hiding this comment

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

Tried out resizeTo but its not implemented. jest incorporates JSDOM to override window - and JSDOM doesn't support all the same methods. Ended up overriding tyepscript and then eslint warnings about overriding typescript 😆 I hadn't realized I could just do that and move on.

@haworku haworku requested a review from eamahanna May 18, 2020 22:52
Copy link
Contributor

@eamahanna eamahanna left a comment

Choose a reason for hiding this comment

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

I just had a couple of questions. Nice work :)

- refactor out FooterExtendedNavList
- add more tests
@haworku haworku force-pushed the hw-footer-mobile branch from 3e0e4b2 to a22f434 Compare May 20, 2020 13:55
Copy link
Contributor

@suzubara suzubara left a comment

Choose a reason for hiding this comment

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

I left some comments but they are pretty minor. This is looking really good!


describe('isMobile', () => {
it.todo(
'renders mobile view when client window width is less than threshold'
Copy link
Contributor

Choose a reason for hiding this comment

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

could you use the resizeTo method instead of reassigning the dimensions? https://developer.mozilla.org/en-US/docs/Web/API/Window/resizeTo

imo this would be a great test to have!


function handleResize(): void {
const isMobileWidth = window.innerWidth < 480
setIsMobile(isMobileWidth)
Copy link
Contributor

Choose a reason for hiding this comment

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

my only suggestion here would be to only call setIsMobile if the value has changed, to avoid extraneous state updates

React.HTMLAttributes<HTMLElement>): React.ReactElement => {
const classes = classnames('grid-row grid-gap-4', className)

const [isMobile, setIsMobile] = React.useState(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is super minor but I noticed there is a brief flash of the desktop nav at mobile sizes on render. I think that could be resolved by doing the initial window size check here to default the state to the correct value (instead of false) and then that would remove the need to trigger handleResize() in the effect hook

- add back useEffect for window resize
- make isMobile be possibly undefined
- adjust tests and remove unncessary story
- prevent unncessary state renders
@haworku
Copy link
Contributor Author

haworku commented May 21, 2020

@suzubara I incorporated the changes you suggested. Tried to rename variables a bit to help clarify the difference between isMobile the prop and then the fallback state based on screen width.

@haworku haworku requested a review from suzubara May 21, 2020 14:51
Copy link
Contributor

@suzubara suzubara 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 but only one is blocking

// Mobile width is less than 480
//eslint-disable-next-line @typescript-eslint/ban-ts-ignore
//@ts-ignore
window.innerWidth = 479
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking: just curious, did you try using window.resizeTo instead of assigning innerWidth? then I don't think we'd run into the TS problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI #181 (comment) Also just double checked it this is the exact error in the console Error: Not implemented: window.resizeTo on running jest.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ooh I see, got it! 👍

if (!isClient) return

function handleResize(): void {
if (isMobileWidth !== isMobileFallback) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think isMobileWidth needs to be re-checked on resize? It doesn't look like this changes when you size up/down

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch I wasn't seeing any issues with my testing and this made me look at again. Definitely need to called window.innerWidth each time in handleResize to get the right behaviors when lots of resizing of the window is happening.

const useMobile = isMobile || (isMobile === undefined && isMobileFallback)

useEffect(() => {
if (!isClient) return
Copy link
Contributor

Choose a reason for hiding this comment

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

non-blocking: just for performance, couldn't we also do an early return if isMobile is defined? since then the fallback isn't used at all and we wouldn't need to attach the event listener.

@haworku haworku requested a review from suzubara May 21, 2020 19:08
@haworku haworku merged commit 188ce99 into hw-footer May 22, 2020
@haworku haworku deleted the hw-footer-mobile branch May 22, 2020 16:17
haworku added a commit that referenced this pull request May 22, 2020
- refactor out FooterExtendedNavList
- add more tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants