项目作者: bartkozal

项目描述 :
Flexbox based responsive fraction grid system
高级语言: JavaScript
项目地址: git://github.com/bartkozal/vue-fraction-grid.git
创建时间: 2017-02-12T14:16:01Z
项目社区:https://github.com/bartkozal/vue-fraction-grid

开源协议:MIT License

下载


vue-fraction-grid

Flexbox based responsive fraction grid system for Vue.js

Live Demo and Full Documentation

  1. <container>
  2. <grid vertical="middle" :rwd="{compact: 'stack'}">
  3. <grid-item size="1/6" :rwd="{compact: '0/1'}">
  4. ...
  5. </grid-item>
  6. <grid-item size="3/6" :rwd="{compact: '1/1'}">
  7. ...
  8. </grid-item>
  9. <grid-item size="2/6" :rwd="{compact: '1/1'}">
  10. ...
  11. </grid-item>
  12. </grid>
  13. </container>

Installation

Install the package using yarn or npm:

  1. $ yarn add vue-fraction-grid
  2. # or
  3. $ npm install --save vue-fraction-grid

Global

Load the plugin by calling Vue.use():

  1. import Vue from "vue";
  2. import VueFractionGrid from "vue-fraction-grid";
  3. Vue.use(VueFractionGrid);

Now you have access in your templates to <container>, <grid> and <grid-item> components.

Local

You may prefer to explicitly import the components in your templates:

  1. <template>
  2. <container>
  3. <grid>
  4. <grid-item size="1/2">
  5. ...
  6. </grid-item>
  7. <grid-item size="1/2">
  8. ...
  9. </grid-item>
  10. </grid>
  11. </container>
  12. </template>
  13. <script>
  14. import Container from "vue-fraction-grid/components/Container";
  15. import Grid from "vue-fraction-grid/components/Grid";
  16. import GridItem from "vue-fraction-grid/components/GridItem";
  17. export default {
  18. components: {
  19. Container,
  20. Grid,
  21. GridItem
  22. }
  23. };
  24. </script>

CDN

Load the script file from CDN:

  1. <div id="root"></div>
  2. <script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.1/vue.min.js"></script>
  3. <script src="//unpkg.com/vue-fraction-grid"></script>
  4. <script>
  5. new Vue({
  6. el: "#root",
  7. template: "<container>...</container>"
  8. });
  9. </script>

Settings

  1. Vue.use(VueFractionGrid, {
  2. container: "1020px",
  3. gutter: "24px",
  4. approach: "mobile-first",
  5. breakpoints: {
  6. compact: "320px 414px"
  7. }
  8. });

Configure grid by passing options as a second argument of Vue.use(). Configuration is merged to defaults that are listed above.

Available settings:

  1. container - Default containers width. Works with any valid CSS values like: '1440px',
  2. '52em', '100vw' etc. Set it to '100%' to use full-width fluid grid. Because
  3. this is the maximum value, the grid will scale nicely for smaller viewports.
  4. gutter - Gutter width. Works with any valid CSS values like '30px', '1rem' etc.
  5. approach - 'mobile-first' or 'desktop-first'. Choose which approach of responsive web design
  6. do you prefer. Breakpoint rules are based on this option.
  7. breakpoints - List the grid breakpoints. Key is the breakpoint name for `:rwd` prop.
  8. Value is the size and can include one or two CSS values separated
  9. with a space.

Components

Container

Syntax:

  1. <container [width="<length>|<percentage>" ]></container>

Center content of the site in the container:

  1. <container>
  2. ...
  3. </container>

Override container’s maximum width with width prop. This is useful when you need more than one container’s type e.g. regular and full-width.

  1. <container width="100%">
  2. ...
  3. </container>
  1. <container width="25vw">
  2. ...
  3. </container>

Grid

Syntax:

  1. <grid
  2. [horizontal="left|center|right"
  3. vertical="top|middle|bottom"
  4. direction="reverse|stack|stack-reverse"
  5. wrap="nowrap|wrap|wrap-reverse"
  6. :rwd="{ breakpointName: direction }"
  7. flat
  8. pair]
  9. ></grid>

The most common usage and simple example of the grid:

  1. <grid>
  2. <grid-item size="1/3">
  3. ...
  4. </grid-item>
  5. <grid-item size="2/3">
  6. ...
  7. </grid-item>
  8. </grid>

Nest grids however you like, the gutter is always the same. There is no need to wrap nested grids with containers.

  1. <grid>
  2. <grid-item size="2/3">
  3. <grid>
  4. <grid-item size="1/2">
  5. ...
  6. </grid-item>
  7. <grid-item size="1/2">
  8. ...
  9. </grid-item>
  10. </grid>
  11. </grid-item>
  12. <grid-item size="1/3">
  13. ...
  14. </grid-item>
  15. </grid>

Horizontal alignment of grid items. This works simillar to justify-content attribute.

  1. <grid horizontal="left">
  2. ...
  3. </grid>
  1. <grid horizontal="center">
  2. ...
  3. </grid>
  1. <grid horizontal="right">
  2. ...
  3. </grid>
  1. <grid horizontal="around">
  2. ...
  3. </grid>
  1. <grid horizontal="between">
  2. ...
  3. </grid>

Vertical alignment of grid items. This works simillar to align-items attribute.

  1. <grid vertical="top">
  2. ...
  3. </grid>
  1. <grid vertical="middle">
  2. ...
  3. </grid>
  1. <grid vertical="bottom">
  2. ...
  3. </grid>

Remove gutter from grid items.

  1. <grid flat>
  2. ...
  3. </grid>

Align content of the first item to the left and content of the second item to the right.

  1. <grid pair>
  2. ...
  3. </grid>

Set the grid items direction. This works simillar to flex-direction attribute.

  1. <grid direction="reverse">
  2. ...
  3. </grid>
  1. <grid direction="stack">
  2. ...
  3. </grid>
  1. <grid direction="stack-reverse">
  2. ...
  3. </grid>

Set the grid items wrap. This works simillar to flex-wrap attribute.

  1. <grid wrap="wrap">
  2. ...
  3. </grid>
  1. <grid wrap="wrap-reverse">
  2. ...
  3. </grid>

Mix props however you want to.

  1. <grid horizontal="right" vertical="bottom" direction="reverse" pair>
  2. ...
  3. </grid>

Grid Item

Syntax:

  1. <grid-item
  2. size="<number>/<number>"
  3. |grow="<number>"
  4. |shrink="<number>"
  5. [:rwd="{ breakpointName: size }"
  6. ]
  7. ></grid-item>

Use any size written in the fraction format. Grid item should be nested directly in the grid. Items fractions don’t have to sum to 1. Denominator can’t be equal 0!

  1. <grid-item size="123/456">
  2. ...
  3. </grid-item>

Fill the grid with a grid item by setting its size to 1/1:

  1. <grid-item size="1/1">
  2. ...
  3. </grid-item>

Hide the grid item by setting its size to 0/1:

  1. <grid-item size="0/1">
  2. ...
  3. </grid-item>

Use grow and shrink props instead of size. They work simillar to flex-grow and flex-shrink attributes. Never mix size, grow and shrink into a single item!

  1. <grid-item grow="2">
  2. ...
  3. </grid-item>
  1. <grid-item shrink="0.5">
  2. ...
  3. </grid-item>

Responsive Design

Responsive design is based on two options from settings: approach and breakpoints.

If you set approach to mobile-first breakpoints with a single value will respond to media queries using min-width attribute. If you use desktop-first instead, breakpoints will respond to max-width.

Breakpoints with two values respond to (min-width: <length>) and (max-width: <length>) query.

Following configuration:

  1. Vue.use(VueFractionGrid, {
  2. approach: "desktop-first",
  3. breakpoints: {
  4. compact: "415px",
  5. tablet: "416px 1100px"
  6. }
  7. });

Is translated to this declaration from CSS:

  1. @media (max-width: 415px) {
  2. ...;
  3. } /* compact */
  4. @media (min-width: 416px) and (max-width: 1100px) {
  5. ...;
  6. } /* tablet */

API

Change the direction of a grid for specific breakpoints.

  1. <grid :rwd="{compact: 'reverse'}">
  2. ...
  3. </grid>
  1. <grid direction="stack" :rwd="{compact: 'stack-reverse'}">
  2. ...
  3. </grid>

Change size of a grid item for specific breakpoints.

  1. <grid-item size="3/4" :rwd="{compact: '0/1'}">
  2. ...
  3. </grid-item>

Mix responsive design props for grid and items.

  1. <grid :rwd="{compact: 'stack'}">
  2. <grid-item size="1/6" :rwd="{tablet: '0/1', compact: '1/1'}">
  3. ...
  4. </grid-item>
  5. <grid-item size="1/2" :rwd="{tablet: '1/2', compact: '0/1'}">
  6. ...
  7. </grid-item>
  8. <grid-item size="1/3" :rwd="{tablet: '1/2', compact: '1/1'}">
  9. ...
  10. </grid-item>
  11. </grid>

Development

  1. Clone the repository:

    1. $ git clone git@github.com:brtjkzl/vue-fraction-grid.git
  2. Run scripts from package.json using npm run / yarn run in the main directory:

    1. demo - Start demo page with implementation of all examples
    2. test - Run tests using Jest
    3. lint - Lint JS and CSS code of components, utils and installation
    4. docs - Start docs locally
    5. deploy - Deploy docs to GitHub Pages
    6. dist - Create an optimized bundle in UMD format

Code is open sourced on GitHub. Up to date changelog is available under the releases section.