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

viewport-addon: Make the addon configurable #3099

Merged
merged 10 commits into from
Mar 20, 2018

Conversation

mshaaban088
Copy link
Contributor

@mshaaban088 mshaaban088 commented Feb 25, 2018

Note: This is my first open-source PR.

Edit

I addressed #2395 and the respective comment

Issue: Currently you cannot customize or add new viewports/devices as described in the README file

What I did

  • Exported a configure function to easily customize the addon
    • Add new device/viewport
    • Replace the entire viewports set
    • Change the default viewport for all stories
  • Exported a decorator function and wrapper component to change the default viewport per story
  • Replaced the default option with a new viewport called responsive to treat the default as 1st-class citizen
  • Added 2 new devices, and different stories for theviewport addon to the official-storybook example
  • Organized the code respectively

Add new device

// config.js
import { configure, INITIAL_VIEWPORTS } from '@storybook/addon-viewport';

const extraViewports = {
  kindleFire2: {
    name: 'Kindle Fire 2',
    styles: {
      width: '600px',
      height: '963px'
    }
  },
  kindleFireHD: {
    name: 'Kindle Fire HD',
    styles: {
      width: '533px',
      height: '801px'
    }
  }
};

configure({
  viewports: {
    ...INITIAL_VIEWPORTS,
    ...extraViewports
  }
});

add

Use new list of devices

// config.js

import { configure } from '@storybook/addon-viewport';

const newViewports = {
  kindleFire2: {
    name: 'Kindle Fire 2',
    styles: {
      width: '600px',
      height: '963px'
    }
  },
  kindleFireHD: {
    name: 'Kindle Fire HD',
    styles: {
      width: '533px',
      height: '801px'
    }
  }
};

configure({
  viewports: newViewports
});

replace

Change the default viewport

// config.js

import { configure } from '@storybook/addon-viewport';

configure({
  defaultViewport: 'iphone6'
});

default-for-all

Change the default viewport per story

// file.stories.js

import React from 'react';
import { storiesOf } from '@storybook/react';
import { withViewport, Viewport } from '@storybook/addon-viewport';

storiesOf('Kindle Fire 2', module)
  .addDecorator(withViewport('kindleFire2'))
  .add('Inherited', () => (
    <div>
      I will be displayed on <b>Kindle Fire 2</b> viewport by default.
    </div>
  ))
  .add('Overridden', () => (
    <Viewport name="iphone6">
      <div>
        I will be displayed on <b>iPhone 6</b> viewport by default.
      </div>
    </Viewport>
  ));

How to test

  1. cd examples/official-storybook
  2. yarn storybook
  3. You will see 2 new devices were added to the viewport panel
  4. Change the extra-viewports.json file
  5. Observe the changes

Is this testable with jest or storyshots?
Yes

Does this need a new example in the kitchen sink apps?
No, I added it to the official-storybook already

Does this need an update to the documentation?
Yes

If your answer is yes to any of these, please make sure to include it in your PR.

Copy link
Member

@igor-dv igor-dv left a comment

Choose a reason for hiding this comment

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

Congrats on your first PR! Great work.


// deprecated usage of infoAddon
import infoAddon from '@storybook/addon-info';

import viewports from './viewports.json';
Copy link
Member

Choose a reason for hiding this comment

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

I think that you need to move the example to the official-storybook. @Hypnosphi ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that would be right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@igor-dv @Hypnosphi I moved it to official-storybook

import addons from '@storybook/addons';
import { ADD_VIEWPORTS_EVENT_ID, SET_VIEWPORTS_EVENT_ID } from '../shared';

export function init() {}
Copy link
Member

Choose a reason for hiding this comment

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

What is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't find a clear API documentation describing the preview API's, so I decided to follow the same approach as manager

Copy link
Member

Choose a reason for hiding this comment

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

Got it, but this method is a bit misleading, so let's remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed it

@codecov
Copy link

codecov bot commented Feb 26, 2018

Codecov Report

Merging #3099 into master will decrease coverage by 0.01%.
The diff coverage is 48.8%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3099      +/-   ##
==========================================
- Coverage    35.9%   35.89%   -0.02%     
==========================================
  Files         437      440       +3     
  Lines        9485     9620     +135     
  Branches      912      906       -6     
==========================================
+ Hits         3406     3453      +47     
- Misses       5500     5572      +72     
- Partials      579      595      +16
Impacted Files Coverage Δ
addons/viewport/src/manager/index.js 83.33% <ø> (ø)
addons/viewport/src/manager/components/styles.js 42.85% <ø> (ø)
.../viewport/src/manager/components/RotateViewport.js 22.72% <ø> (ø)
addons/viewport/preview.js 100% <100%> (ø)
addons/viewport/src/shared/index.js 100% <100%> (ø)
.../viewport/src/manager/components/SelectViewport.js 18.18% <33.33%> (ø)
addons/viewport/src/manager/components/Panel.js 36.02% <41.17%> (ø)
...ns/viewport/src/manager/components/viewportInfo.js 33.33% <45%> (ø)
addons/viewport/src/preview/index.js 88.88% <88.88%> (ø)
app/vue/src/server/utils.js 0% <0%> (-100%) ⬇️
... and 78 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c3d9a41...af02d9a. Read the comment docs.

@mshaaban088
Copy link
Contributor Author

@igor-dv @Hypnosphi thanks for reviewing it


this.setState({
viewports: {
...initialViewports,
Copy link
Member

Choose a reason for hiding this comment

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

Should it be current viewports instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My intention was to make add works by adding new viewports to the initial ones, but indeed your point makes more sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@Hypnosphi Hypnosphi left a comment

Choose a reason for hiding this comment

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

Please update addon documentation

@mshaaban088
Copy link
Contributor Author

@Hypnosphi I updated the documentation and addressed your comment regarding using previous viewports instead of initialViewports

@igor-dv
Copy link
Member

igor-dv commented Feb 27, 2018

@shilman, are we still merging the features? You wanted to lock in favor of the release.

@Hypnosphi
Copy link
Member

Hypnosphi commented Feb 27, 2018

@shilman if we're feature-frozen, we probably should edit those lines on master and add 'feature request' there: https://github.com/storybooks/storybook/blob/master/dangerfile.js#L13-L15

@mshaaban088
Copy link
Contributor Author

I'm going to rebase with master again to not have those Merge branch master into master

@Hypnosphi
Copy link
Member

to not have those Merge branch master into master

We're actually OK with those

@mshaaban088 mshaaban088 force-pushed the master branch 4 times, most recently from 915aca0 to e2ed8ca Compare March 9, 2018 14:31
@mshaaban088 mshaaban088 changed the title viewport-addon: Add support for adding new viewports/devices viewport-addon: Allow configuring the addon Mar 9, 2018
@mshaaban088
Copy link
Contributor Author

mshaaban088 commented Mar 11, 2018

@igor-dv @Hypnosphi @danielduan @ndelangen Could you please re-review it, apart from the conflicts, do you think this way of implementing it is better?
I really appreciate your time reviewing the initial one, but wanted to make everybody happy

Copy link
Member

@Hypnosphi Hypnosphi left a comment

Choose a reason for hiding this comment

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

LGTM

@Hypnosphi
Copy link
Member

Please fix eslint warnings and unit tests

@mshaaban088 mshaaban088 force-pushed the master branch 4 times, most recently from 7e8b611 to 63d11ce Compare March 12, 2018 00:15
@mshaaban088
Copy link
Contributor Author

@Hypnosphi Are you aware of any historical reason behind making viewports an object instead of array?
CC: @saponifi3d

@Hypnosphi
Copy link
Member

No I'm not. I think we're free to change it in this PR as it wasn't exposed to user before. On the other side, it may be convenient as it is now, if user wants to pick only particular viewports from defaults or override some values in a particular viewport:

const { iphone6, ipad } = INITIAL_VIEWPORTS;

configure({
  viewports: {
    iphone6,
    ipad,
    ...newViewports
  }
});
configure({
  viewports: {
    ...INITIAL_VIEWPORTS,
    ipad: {
      ...INITIAL_VIEWPORTS.ipad,
      ...styles: {
        ...INITIAL_VIEWPORTS.ipad.styles,
        height: '1000px'
      }
    },
    ...newViewports
  }
});

@mshaaban088 mshaaban088 changed the title viewport-addon: Allow configuring the addon viewport-addon: Make the addon configurable Mar 12, 2018
@mshaaban088
Copy link
Contributor Author

@Hypnosphi This is absolutely a valid use-case.

The reason why this question is popped up, is that the order of Object.keys is not guaranteed, hence, we cannot say we will pick the first one as default which makes the tests flickering as well.

@Hypnosphi
Copy link
Member

@mshaaban088 can you please resolve the conflict?

@mshaaban088
Copy link
Contributor Author

@Hypnosphi sure

 - By default, viewport addon is reading both initial viewports' set and default viewport from
`INITIAL_VIEWPORTS` and `DEFAULT_VIEWPORT` respectively which are exported by the addon
 - `configure` function is exported which accepts an object with `defaultViewport` and `viewports`
 - Change the default viewport by setting `defaultViewport` property to the key of the viewport (e.g. `configure({ defaultViewport: 'iphone5' })`)
 - Replace the entire viewports by setting brand new viewports to `viewports` property (e.g. `configure({ viewports: { brandNewDevice: { ... } } })`)
 - Add custom viewports by first merging both the `INITIAL_VIEWPORTS` and your custom viewports, and then pass the result as `viewports` to configure function
@mshaaban088
Copy link
Contributor Author

@Hypnosphi done

@Hypnosphi Hypnosphi merged commit b2f0115 into storybookjs:master Mar 20, 2018
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.

5 participants