Categories
Vuetify

Vuetify — Text Styles

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Typography

We can add classes for typography.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <div class="text-h1">heading 1</div>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the text-h1 class to render a large heading.

We can do the same for the other headings, body, button, caption, subtitle, overline with the class with the same names.

Text Decoration

Vuetify also provides text-decoration classes to set this CSS property.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col col="12">
        <a href="#" class="text-decoration-none">Non-underlined link</a>
        <div class="text-decoration-line-through">Line-through text</div>
        <div class="text-decoration-overline">Overline text</div>
        <div class="text-decoration-underline">Underline text</div>
      </v-col>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({}),
};
</script>

to style text with or without lines.

Text Transform

We can transform text with some text- classes.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col col="12">
        <p class="text-lowercase">Lowercased text.</p>
        <p class="text-uppercase">Uppercased text.</p>
        <p class="text-capitalize">CapiTaliZed text.</p>
      </v-col>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({}),
};
</script>

We can change the text to upper case, lower case, or capitalized as we wish.

Font Weights and Italics

We can change the font-weight and toggle on or off italics with classes provided by Vuwetify.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col col="12">
        <p class="font-weight-black">Black text.</p>
        <p class="font-weight-bold">Bold text.</p>
        <p class="font-weight-medium">Medium weight text.</p>
        <p class="font-weight-regular">Normal weight text.</p>
        <p class="font-weight-light">Light weight text.</p>
        <p class="font-weight-thin">Thin weight text.</p>
        <p class="font-italic">Italic text.</p>
      </v-col>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({}),
};
</script>

The font-weight- classes let us set the font-weight to what we want.

Text Opacity

The text opacity can be changed with different classes.

For instance, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col col="12">
        <p class="text--primary">opacity 87%.</p>
        <p class="text--secondary">opacity 60%.</p>
        <p class="text--disabled">opacity 38%.</p>
      </v-col>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({}),
};
</script>

The opacity can be changed with the text--primary , text--secondary and text--disabled classes.

They have the opacities listed.

RTL Alignment

Vuetify has classes to change the alignment to RTL.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col col="12">
        <p class="subtitle-2 text-center">lorem ipsum</p>
        <p
          class="text-sm-left"
        >lorem ipsum</p>
        <p
          class="text-md-left"
        >lorem ipsum.</p>
        <p
          class="text-lg-right"
        >lorem ipsum.</p>
        <p
          class="text-xl-left"
        >lorem ipsum</p>
        <p class="subtitle-2 text-center">lorem ipsum</p>
        <p class="text-start">lorem ipsum</p>
        <p class="text-end">lorem ipsum</p>
      </v-col>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({}),
};
</script>

We have the classes to align text according to breakpoints and the position.

Conclusion

We can align text and change text style with the classes provided by Vuetify.

Categories
Vuetify

Vuetify — Paddings and Margins

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Flex align-content

We can add the align-content classes to change the alignment of flexbox content.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          class="d-flex align-content-start flex-wrap"
          color="grey lighten-2"
          flat
          tile
          min-height="200"
        >
          <v-card v-for="n in 10" :key="n" class="pa-2" outlined tile>item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the align-content-start class to align the content to the left.

There are other classes for other alignments.

Flex Grow and Shrink

Vuetify has the flex-{condition}-{value} classes to grow and shrink the items.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="2" class="flex-grow-0 flex-shrink-0">
        <v-card class="pa-2" outlined tile>2 column wide</v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

to create a column 2 columns wide and use the flex-grow-0 and flex-shrink-0 classes to fill the container.

Float

We can apply a custom float with Vuetify classes.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <div>
          <div class="float-left">Float left</div>
          <br />
          <div class="float-right">Float right</div>
          <br />
          <div class="float-none">Don't float</div>
        </div>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have float-left class to float left.

float-right floats right.

float-none disables float.

Responsive

Floats can be applied with some breakpoints.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <div>
          <div class="float-sm-left">sm</div>
          <br />
          <div class="float-md-left">md</div>
          <br />
          <div class="float-lg-left">lg</div>
          <br />
          <div class="float-xl-left">xl</div>
          <br />
        </div>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the breakpoint in the class name to only apply the float if the breakpoint is the given one or wider.

Spacing

Spacing can be changed with classes in the following format:

{property}{direction}-{size}

property can be:

  • m – applies margin
  • p – applies padding

direction can be:

  • t – applies the spacing for margin-top and padding-top
  • b – applies the spacing for margin-bottom and padding-bottom
  • l – applies the spacing for margin-left and padding-left
  • r – applies the spacing for margin-right and padding-right
  • s – applies the spacing for margin-left/padding-left (in LTR mode) and margin-right/padding-right (in RTL mode)
  • e – applies the spacing for margin-right/padding-right (in LTR mode) and margin-left/padding-left (in RTL mode)
  • x – applies the spacing for both *-left and *-right
  • y – applies the spacing for both *-top and *-bottom
  • a – applies the spacing for the property in all directions

size is 0 to 16 to set the size from 0 to 64px.

n1 to n16 set the margin to -4px to -64px.

We can use mx-auto to set the margin size automatically:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card class="mx-auto" color="white" width="200px">
          <v-card-text>Centered</v-card-text>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We can change the padding by writing:

<template>
  <v-container>
    <v-row class="text-center">
      <v-card class="pa-md-4 mx-lg-auto" color="white" width="250px">
        <v-card-text>text</v-card-text>
      </v-card>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We have the pa-md-4 class to change the padding for md breakpoint and up to 16px.

Conclusion

Vuetify provides us with many classes for changing margins and padding.

Categories
Vuetify

Vuetify — Flexbox

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Flex Justify

We can justify-content in a flexbox container with the classes provided by Vuetify.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          v-for="j in justify"
          :key="j"
          :class="`d-flex justify-${j} mb-2`"
          color="grey lighten-2"
          flat
          tile
        >
          <v-card v-for="n in 3" :key="n" class="pa-2" outlined tile>item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    justify: ["start", "end", "center", "space-between", "space-around"],
  }),
};
</script>

to set the justification of our content to what we want.

There are also responsive variants of the classes.

Flex Align

Vuetify also provides us with flex align classes to align content.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          v-for="a in align"
          :key="a"
          :class="`d-flex align-${a} mb-6`"
          flat
          height="100"
          tile
        >
          <v-card v-for="n in 3" :key="n" class="pa-2" outlined tile>item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    align: ["start", "end", "center", "baseline", "stretch"],
  }),
};
</script>

to align our content our way.

We have the d-flex and align classes to align the containers in various ways.

Flex Align Self

We can use the align-self classes to apply the align-self property.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card
          v-for="j in justify"
          :key="j"
          class="d-flex mb-6"
          height="100"
        >
          <v-card
            v-for="n in 3"
            :key="n"
            class="pa-2"
            :class="[n === 1 && `align-self-${j}`]"
          >{{ n === 1 ? 'Aligned flex item' : 'Flex item' }}</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    justify: ["start", "end", "center", "baseline", "auto", "stretch"],
  }),
};
</script>

We have the align-self classes to align the classes our way.

Flex Wrap

Vuetify also provides the flex wrap classes.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card class="d-flex flex-nowrap py-3" color="grey lighten-2" flat tile>
          <v-card v-for="n in 5" :key="n" class="pa-2" outlined tile>Flex item</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
    justify: ["start", "end", "center", "baseline", "auto", "stretch"],
  }),
};
</script>

We have the flex-nowrap to prevent wrapping.

The general format of the classes is flex-{breakpoint}-{condition} .

Flex Order

We can switch the visual order of the flex items with the order classes.

So we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <v-card class="d-flex flex-wrap-reverse" color="grey lighten-2" flat tile>
          <v-card class="order-3 pa-2" outlined tile>First</v-card>
          <v-card class="order-2 pa-2" outlined tile>Second</v-card>
          <v-card class="order-1 pa-2" outlined tile>Third</v-card>
        </v-card>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({
  }),
};
</script>

We flipped the order with the flex-wrap-reverse class.

Conclusion

Vuetify provides us with various flexbox utilities to lay out our items.

Categories
Vuetify

Getting Started with Vuetify

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to get started with the Vuetify framework.

Vue CLI Install

The easiest way to create a Vuetify Vue app is to use the Vue CLI.

We create a project directory, go into it, and run:

npx vue create .

We choose the options we want when prompted.

Once we did that, we run:

vue add vuetify

to add the Vuetify files.

We choose Default when prompted.

This is the recommended configuration.

It comes with all the components and assets we need to build an app with it.

Colors

Once we did that, we can set the colors by using the classes.

So we can write:

<div class="red">

or

<span class="red--text">

and to set the colors to red.

We can also change the colors in vuetify.js :

import Vue from 'vue';
import Vuetify from 'vuetify/lib';
import colors from 'vuetify/lib/util/colors'

Vue.use(Vuetify);

export default new Vuetify({
  theme: {
    themes: {
      light: {
        primary: colors.red.darken1,
        secondary: colors.red.lighten4,
        accent: colors.indigo.base
      },
    },
  },
});

We import the colors object and set the colors to what we want.

SASS Colors

We can also import SASS colors by writing:

@import '~vuetify/src/styles/main.sass';

They can also be imported in the src/index.js file writing;

import './src/sass/main.scss'
// OR
require('./src/sass/main.scss')

This works if we configured our Vue CLI to use SASS.

Content

We can create content with various elements.

Vuetify provides styles for them.

For instance, we can create a blockquote by writing:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <blockquote class="blockquote">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</blockquote>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We just put them inside a component within the v-container to have the styling applied.

We can do the same for code:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
         <code>&lt;code&gt;</code>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

Also, we can apply styles to the kbd element:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <kbd>npm install vuetify</kbd>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

Display Helpers

We can add various classes to add padding, colors, set the display CSS property and more.

For example, we can write:

<template>
  <v-container>
    <v-row class="text-center">
      <v-col cols="12">
        <div class="d-inline pa-3 deep-purple accent-4 white--text">foo</div>
        <div class="d-inline pa-3 black white--text">bar</div>
      </v-col>
    </v-row>
  </v-container>
</template>

<script>
export default {
  name: "HelloWorld",

  data: () => ({}),
};
</script>

We create a purple and black divs with some classes.

pa-3 created padding.

d-inline make the divs display inline.

deep-purple , accent-4 , white--text , and black are the color classes.

Conclusion

We can create a Vuetify app with a few commands.

It adds styles to elements and we can also add our own style with the classes it provides.

Categories
React Tips

React Tips — Disable Buttons, FormData, Types for Function

React is a popular library for creating web apps and mobile apps.

In this article, we’ll look at some tips for writing better React apps.

Send Multipart Form Data with Axios in a React Component

We can send form data with the FormData constructor.

We can pass that straight into the Axios post method.

For instance, we can write:

import React from 'react'
import axios, { post } from 'axios';

class App extends React.Component {

  constructor(props) {
    super(props);
    this.state ={
      file:null
    }
    this.onFormSubmit = this.onFormSubmit.bind(this)
    this.onChange = this.onChange.bind(this)
    this.fileUpload = this.fileUpload.bind(this)
  }

  onFormSubmit(e){
    e.preventDefault()
    const url = 'http://example.com/upload';
    const formData = new FormData();
    formData.append('file', this.state.file);
    const config = {
      headers: {
        'content-type': 'multipart/form-data'
      }
    }
    post(url, formData, config);
      .then((response) => {
        console.log(response.data);
      })
  }

  onChange(e) {
    this.setState({ file: e.target.files[0 ]})
  }

  render() {
    return (
      <form onSubmit={this.onFormSubmit}>
        <h1>File Upload</h1>
        <input type="file" onChange={this.onChange} />
        <button type="submit">Upload</button>
      </form>
   )
  }
}

We have a file input, where we set the file input to the file that’s submitted in the onChange method.

We save the selected file object as the value of the file state.

Then when we click the Upload button, onFormSubmit is run.

In the method, we created a FomrData instance.

Then we append our file into the FormData instance.

We also set the header so that we indicate that we’re sending form data.

Once we did that, we proceed with our file upload.

Disable Button with React

We can disable a button with React by setting the disabled prop of the button.

For instance, we can write:

<button disabled={!this.state.value} />

We can use it in a component by writing:

class ItemForm extends React.Component {
  constructor() {
    super();
    this.state = { value: '' };
    this.onChange = this.onChange.bind(this);
    this.add = this.add.bind(this);
  }

  add() {
    this.props.addItem(this.state.value);
    this.setState({ value: '' });
  }

  onChange(e) {
    this.setState({ value: e.target.value });
  }

  render() {
    return (
      <div>
        <input
          type="text"
          value={this.state.value}
          onChange={this.onChange}
          placeholder='item name'
        />
        <button
          disabled={!this.state.value}
          onClick={this.add}
        >
          Add
        </button>
      </div>
    );
  }

We pass in the value state to let us enter the data that we want into the input field.

Then we check that in disabled prop of the button.

This way, the button is disabled if we have nothing inputted into the form field.

Cannot Read Property ‘map’ of Undefined

We may get this if we try to call map on something that doesn’t have a map method.

This may happen to something that we expect to be an array, but it’s not.

Therefore, we should check if the value we’re calling map on is an array before doing anything.

For instance, we can do that by writing:

if (Array.isArray(this.props.data)) {
  const commentNodes = this.props.data.map((comment) => {
    return (
      <div>
        <h1>{comment.title}</h1>
      </div>
    );
  });
}

We called Array.isArray to check if this.props.data is an array.

If it is, then we call map to map the data prop to h1 elements.

How to Add Custom HTML Attributes in JSX

With React 16, we can add custom attributes natively.

For instance, we can write:

render() {
  return (
    <div data-foo="bar" />
  );
}

We can just add it straight into our HTML elements without doing anything special.

Use Children with React Stateless Functional Component in TypeScript

We can pass in the interface or type alias into the generic type argument of React.FunctionComponent to set the type for ur props.

As long as the alias or interface has the children prop, we can use the children prop.

For instance, we can write:

const Foo: React.FunctionComponent<FooProps> = props => (
  <div>
    <p>{props.bar}</p>
    <p>{props.children}</p>
  </div>
);

FooProps has the bar and children entries, so we can reference both in our component.

React.FC is the shorthand for React.FunctionComponent .

Before React 16.8, we use the React.StatelessComponent type instead.

For instance, we can write:

const Foo: React.StatelessComponent<{}> = props => (
  <div>{props.children}</div>
);

or:

const Foo : React.StatelessComponent<FooProps> = props => (
  <div>
    <p>{props.propInMyProps}</p>
    <p>{props.children}</p>
  </div>
);

React.SFC is the shorthand for React.StatelessComponent.

Conclusion

We can add types to stateless components with TypeScript.

Sending multipart form data can be done with the FormData constructor.

Also, we should make sure that we have an array before calling map on it.

We can conditionally disable buttons with React.