Categories
NativeScript React

NativeScript React — Dock and Flexbox Layouts

React is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript React.

Dock Multiple Children to the Same Side

We can dock multiple child components on the same side.

For example, we can write:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <dockLayout stretchLastChild backgroundColor="#3c495e">
      <label text="left 1" dock="left" width={40} backgroundColor="red" />
      <label text="left 2" dock="left" width={40} backgroundColor="green" />
      <label text="left 3" dock="left" width={40} backgroundColor="blue" />
      <label text="last child" backgroundColor="yellow" />
    </dockLayout>
  );
}

We set dock to left on the first 3 label s, so we have them display side by side.

FlexboxLayout

We can add a flexboxLayout component to arrange child components with flexbox CSS properties.

For example, we can write:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <flexboxLayout backgroundColor="#3c495e">
      <label text="first" width={70} backgroundColor="red" />
      <label text="second" width={70} backgroundColor="green" />
      <label text="third" width={70} backgroundColor="blue" />
    </flexboxLayout>
  );
}

We add the label s and they’ll be displayed side by side.

Also, we can set the flexDirection to 'column' so that we stack the child components:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <flexboxLayout flexDirection="column" backgroundColor="#3c495e">
      <label text="first" height={70} backgroundColor="red" />
      <label text="second" height={70} backgroundColor="green" />
      <label text="third" height={70} backgroundColor="blue" />
    </flexboxLayout>
  );
}

We set the alignItems prop to align the items the way we want in the flexboxLayout container:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <flexboxLayout alignItems="flex-start" backgroundColor="#3c495e">
      <label text="first" width={70} height={70} backgroundColor="red" />
      <label text="second" width={70} height={70} backgroundColor="green" />
      <label text="third" width={70} height={70} backgroundColor="blue" />
    </flexboxLayout>
  );
}

We set alignItems to flex-start to put the label s side by side in the top left corner.

Also, we can set the order prop by writing:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <flexboxLayout alignItems="flex-start" backgroundColor="#3c495e">
      <label text="first" order={2} width={70} height={70} backgroundColor="red" />
      <label text="second" order={3} width={70} height={70} backgroundColor="green" />
      <label text="third" order={1} width={70} height={70} backgroundColor="blue" />
    </flexboxLayout>
  );
}

This way, we switch the order of the label s.

Rows can be wrapped if we set the flexWrap prop to 'wrap' :

import * as React from "react";

export default function Greeting({ }) {
  return (
    <flexboxLayout flexWrap="wrap" backgroundColor="#3c495e">
      <label text="first" width='30%' backgroundColor="red" />
      <label text="second" width='30%' backgroundColor="green" />
      <label text="third" width='30%' backgroundColor="blue" />
      <label text="fourth" width='30%' backgroundColor="yellow" />
    </flexboxLayout>
  );
}

We set the width of each label to 30%, so the last one will show in the 2nd row.

Conclusion

We can add dock and flex layouts into our mobile app with NativeScript React.

Categories
NativeScript React

Getting Started with Mobile Development with NativeScript React

React is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript React.

Install NativeScript

We start by install the nativescript Node package globally by running:

npm install -g nativescript

Create the App Project

Once we installed the package, we create the project by writing:

tns create my-blank-react --react

Then we can run tns run android to run the project after going into the folder.

This should be done as an admin user. Then we can select Configure for Local Build and let it install all the packages that are required to run the project.

If Genymotion is started, then you should see the project.

First App

Once we create the project, then we should see the AppContainer.tsx file.

It is the entry point component for our app.

And it has the following code:

import * as React from "react";
import { Dialogs } from "@nativescript/core";

export default function Greeting({ }) {
  return (
    <gridLayout
      width={"100%"}
      height={"100%"}
      rows={"*, auto, auto, *"}
      columns={"*, 200, *"}
    >
      <label
        row={1}
        col={1}
        className="info"
        textAlignment={"center"}
        fontSize={24}
      >
        <formattedString>
          <span className="fas" text="&#xf135;" />
          <span> Hello World!</span>
        </formattedString>
      </label>
      <button
        row={2}
        col={1}
        fontSize={24}
        textAlignment={"center"}
        onTap={() => Dialogs.alert("Tap received!")}
      >
        Tap me
      </button>
    </gridLayout>
  );
}

We see a button to open a dialog.

The dialog is displayed with the Dialogs.alert method.

AbsoluteLayout

We can add position components on a page with absoluteLayout .

For example, we can write:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <absoluteLayout backgroundColor="#3c495e">
      <label text="10,10" left={10} top={10} width={100} height={100} backgroundColor="red" />
      <label text="120,10" left={120} top={10} width={100} height={100} backgroundColor="green" />
      <label text="10,120" left={10} top={120} width={100} height={100} backgroundColor="blue" />
      <label text="120,120" left={120} top={120} width={100} height={100} backgroundColor="yellow" />
    </absoluteLayout>
  );
}

We add the absoluteLayout component to add our layout.

Then we add the label components inside it to show boxes with the given background color, width, and height.

left and top sets the x and y coordinates of the top left corner of the components.

We can add components that overlap with absoluteLayout .

For example, we can write:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <absoluteLayout backgroundColor="#3c495e">
      <label text="10,10" left={10} top={10} width={100} height={100} backgroundColor="red" />
      <label text="30,40" left={30} top={40} width={100} height={100} backgroundColor="yellow" />
    </absoluteLayout>
  );
}

We set the left and top props so that the label s overlap each other.

DockLayout

The dockLayout lets us add a layout where the components snap to the left, right, top, or bottom of the screen.

For example, we can write:

import * as React from "react";

export default function Greeting({ }) {
  return (
    <dockLayout stretchLastChild={false} backgroundColor="#3c495e">
      <label text="left" dock="left" width={40} backgroundColor="red" />
      <label text="top" dock="top" height={40} backgroundColor="green" />
      <label text="right" dock="right" width={40} backgroundColor="blue" />
      <label text="bottom" dock="bottom" height={40} backgroundColor="yellow" />
    </dockLayout>
  );
}

to add label s to the edges of the screen.

The dock prop sets the location that the label s are docked to.

stretchLastChild lets us stretch the last child component to the nearest components if it’s true .

If we set stretchLastChild to true :

import * as React from "react";

export default function Greeting({ }) {
  return (
    <dockLayout stretchLastChild backgroundColor="#3c495e">
      <label text="left" dock="left" width={40} backgroundColor="red" />
      <label text="top" dock="top" height={40} backgroundColor="green" />
      <label text="right" dock="right" width={40} backgroundColor="blue" />
      <label text="bottom" dock="bottom" backgroundColor="yellow" />
    </dockLayout>
  );
}

Then the ‘bottom’ label is stretched to meet the other label s.

Conclusion

We can create a simple mobile app with various layouts in our mobile app with NativeScript React.

Categories
NativeScript Vue

NativeScript Vue — Full Screen Modal and Tabs

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

Full Screen Modal

We can show a full-screen modal by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Modal" @tap="openModal" />
    </FlexboxLayout>
  </Page>
</template>

<script>
const Detail = {
  props: ["id"],
  template: `
    <Page>
      <ActionBar title="Detail"/>
      <StackLayout>
        <Label :text="id" />
        <Button @tap="$modal.close" text="Close" />
      </StackLayout>
    </Page>
  `,
};
export default {
  methods: {
    openModal() {
      this.$showModal(Detail, { fullscreen: true, props: { id: 1 } });
    },
  },
};
</script>

We call the $showModal method with the Detail component.

And we set the fullscreen property to true to make the modal full screen.

Return Data from the Modal

We can return data from the modal.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Modal" @tap="openModal" />
    </FlexboxLayout>
  </Page>
</template>

<script>
const Detail = {
  props: ["id"],
  template: `
    <Page>
      <ActionBar title="Detail"/>
      <StackLayout>
        <Label text="Detail" />
        <Button @tap="$modal.close('foo')" text="Close" />
      </StackLayout>
    </Page>
  `,
};
export default {
  methods: {
    async openModal() {
      const data = await this.$showModal(Detail);
      console.log(data);
    },
  },
};
</script>

In the Detail component, we pass in the 'foo' argument in the $modal.close method.

Then we can get that value as the resolved value of the promise returned by $showModal .

BottomNavigation

We can add a navigation bar to the bottom of our screen by writing:

src/mainl.js

import Vue from 'nativescript-vue'
import App from './components/App'
import VueDevtools from 'nativescript-vue-devtools'

if (TNS_ENV !== 'production') {
  Vue.use(VueDevtools)
}

// Prints Vue logs when --env.production is *NOT* set while building
Vue.config.silent = (TNS_ENV === 'production')

new Vue({
  render: h => h(App)
}).$start()

src/components/App.vue

<template>
  <Page>
    <FlexboxLayout flexDirection="column">
      <BottomNavigation>
        <TabStrip>
          <TabStripItem>
            <Label text="Home"></Label>
          </TabStripItem>
          <TabStripItem>
            <Label text="Browse"></Label>
          </TabStripItem>
          <TabStripItem>
            <Label text="Search"></Label>
          </TabStripItem>
        </TabStrip>
        <TabContentItem>
          <Frame id="homeTabFrame">
            <Page>
              <Label text="home" />
            </Page>
          </Frame>
        </TabContentItem>
        <TabContentItem>
          <Frame id="browseTabFrame">
            <Page>
              <Label text="browse" />
            </Page>
          </Frame>
        </TabContentItem>
        <TabContentItem>
          <Frame id="searchTabFrame">
            <Page>
              <Label text="search" />
            </Page>
          </Frame>
        </TabContentItem>
      </BottomNavigation>
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {};
</script>

We render the App component in main.js .

Then in the App component, we add the BottomNavigation component to add the bottom navigation bar.

The TabStrip has the navigation links.

TabStripItem has the content for the tabs.

TabContentItem has the content for the tabs.

The TabContentItem s have the Frame s to show in the tab.

Conclusion

We can add full-screen modals and tabs into our mobile app with NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Navigation and Modals

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

Navigation

We can use the $navigateTo method lets us show different components in our app.

For example, we can write:

components/App.vue

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Go to Detail" @tap="goTo" />
    </FlexboxLayout>
  </Page>
</template>

<script>
import Detail from "@/components/Detail";

export default {
  methods: {
    goTo() {
      this.$navigateTo(Detail);
    },
  },
};
</script>

components/Detail.vue

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Go to App" @tap="goTo" />
    </FlexboxLayout>
  </Page>
</template>

<script>
import App from "@/components/App";

export default {
  methods: {
    goTo() {
      this.$navigateTo(App);
    },
  },
};
</script>

In the App component, we call this.$navigateTo with the Detail component to remove App from the screen show the Detail component.

Likewise, in the Detail component, we call the same method with the App component to replace the Detail component with the App component.

We can pass props to the component.

For instance, we can write:

components/App.vue

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Go to Detail" @tap="goTo" />
    </FlexboxLayout>
  </Page>
</template>

<script>
import Detail from "@/components/Detail";

export default {
  methods: {
    goTo() {
      this.$navigateTo(Detail, {
        props: {
          foo: "bar",
        },
      });
    },
  },
};
</script>

components/Detail.vue

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Go to App" @tap="goTo" />
      <Label :text="foo" style="text-align: center" />
    </FlexboxLayout>
  </Page>
</template>

<script>
import App from "@/components/App";

export default {
  props: ["foo"],
  methods: {
    goTo() {
      this.$navigateTo(App);
    },
  },
};
</script>

In the App component, we pass in an object into the 2nd argument with the props property to pass in props.

Then in the Detail component, we accept the prop as the props property.

And then we get its value and display it in the Label .

We can go back to the previous page with the $navigateBack method.

To use it, we can write:

components/App.vue

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Go to Detail" @tap="goTo" />
    </FlexboxLayout>
  </Page>
</template>

<script>
import Detail from "@/components/Detail";

export default {
  methods: {
    goTo() {
      this.$navigateTo(Detail);
    },
  },
};
</script>

components/Detail.vue

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Go Back" @tap="goTo" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    goTo() {
      this.$navigateBack();
    },
  },
};
</script>

We have the Detail component that calls the this.$navigateBack method when we tap on the button.

Modals

We can use the this.$showModal method to show a modal.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Modal" @tap="openModal" />
    </FlexboxLayout>
  </Page>
</template>

<script>
const Detail = {
  props: ["id"],
  template: `
    <Page>
      <ActionBar title="Detail"/>
      <StackLayout>
        <Label :text="id" />
        <Button @tap="$modal.close" text="Close" />
      </StackLayout>
    </Page>
  `,
};

export default {
  methods: {
    openModal() {
      this.$showModal(Detail, { props: { id: 1 } });
    },
  },
};
</script>

We call the $showModal method with the Detail component.

And we can pass in props by setting the props property.

Conclusion

We can add navigation and show modals into our mobile app with NativeScript Vue.

Categories
NativeScript Vue

NativeScript Vue — Login and Prompt Dialogs

Vue is an easy to use framework for building front end apps.

NativeScript is a mobile app framework that lets us build native mobile apps with popular front end frameworks.

In this article, we’ll look at how to build an app with NativeScript Vue.

LoginDialog

NativeScript Vue comes with a login dialog.

For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await login("Login", "Username field", "Password field");
      const { result: res, userName, password } = result;
      console.log(res, userName, password);
    },
  },
};
</script>

We have a button to open the login dialog.

The login function opens the login dialog.

The first argument is the message content.

The 2nd and 3rd arguments are the placeholders for the username and password inputs respectively.

Then we can get the values of the username and password from the resolved value.

res has the form validation result.

We can set more options by writing:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await login({
        title: "Login Title",
        message: "Login message",
        okButtonText: "OK",
        cancelButtonText: "Cancel",
        userName: "user",
        password: "password",
      });
      const { result: res, userName, password } = result;
      console.log(res, userName, password);
    },
  },
};
</script>

title has the login dialog title.

message has the message for the login dialog.

okButtonText sets the OK button’s text.

cancelButtonText sets the cancel button’s text.

userName sets the default value of the username field.

And password sets the default value of the password field.

PromptDialog

The prompt global function lets us open a dialog with a single-line text input.

To use it, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await prompt(
        "Enter something",
        "Suggested user input"
      );
      console.log(result);
    },
  },
};
</script>

We have a button to open the prompt dialog.

The prompt method takes the dialog text as the first argument and the default value for the input as the 2nd argument.

We can also add more options. For example, we can write:

<template>
  <Page>
    <ActionBar title="NativeScript App"></ActionBar>
    <FlexboxLayout flexDirection="column">
      <Button text="Open Dialog" @tap="openDialog" />
    </FlexboxLayout>
  </Page>
</template>

<script>
export default {
  methods: {
    async openDialog() {
      const result = await prompt({
        title: "Title",
        message: "Enter something",
        okButtonText: "OK",
        cancelButtonText: "Cancel",
        defaultText: "Suggested value",
      });
      console.log(result);
    },
  },
};
</script>

We set the title value to show the dialog title.

message has the dialog message.

okButtonText has the OK button text.

cancelButtonText has the cancel button text.

defaultText has the default value for the input.

Conclusion

We can add a login and prompt dialog into our mobile app with NativeScript Vue.