Window Size – Add responsive behavior to your components

The Window-Size function is an add-on that can be downloaded from the Squirrel365 Marketplace.

Find more details about other available add-ons on the Squirrel365 Marketplace page.

Adding responsive behavior to your components

Now that we have our Window Size add-on wired up, we can begin converting or building our application into something that responds as the canvas size changes. In this How-To, I will be constructing a project from scratch, applying the responsive behavior with the Window Size function applied.

If you haven’t seen the first article on this, check it out here

Simple Example – Setting up components with varying sizes

Intermediate Example – Setting up components with varying sizes AND positions

Expert Example – Adjusting components for various canvas sizes

Canvas size and component placement

To begin with, we want to begin by placing our components. Depending on the type of application you are building, you may have different components and a different layout, but the principles remain the same. Another area that you will need to consider and explore is the size of the initial canvas. You could be looking to display your application in both desktop and mobile formats. In this instance, you will want to look at designing layouts that suit both of those environments.

Check out this external article, ‘What Size Should Your Dashboard Be?‘ for tips and tricks on getting started.

Once you have your canvas sizes ready, and your designs in place, now we just need to set up the components in Squirrel to become responsive to the transition between those sizes.

Setting up the canvas

Determining the size of the canvas could be difficult as there are so many varying sizes to choose from that match certain screen sizes. To aid this decision, Squirrel offers a series of default canvas sizes that you can find in the product’s ‘Start Page’

For the sake of this article, we will be using these standard sizes just as a reference – feel free to use your own.

We will be exploring two canvas sizes in this article: Computer and Tablet.

Computer – 1366 x 768

Tablet – 1024 x 768

We are going to go ahead and place these values in the spreadsheet and wire up our Window Size component. See this article for more information on setting up the add-on.

Simple Example – Setting up components with varying sizes

This simple example will demonstrate a foundation example of the formulas used across the rest of the project.

  1. Place a Rectangle component on the canvas

  2. Set the XY position to 0,0

  3. Bind the width of the Rectangle to the width of the canvas on the spreadsheet

Tip: Highlight your cells to make them easier to discern in the spreadsheet. In this example, we will be highlighting cells that will be changing yellow (either through formula or insertion via the Window Size add-on) and static values will be highlighted green (these values will not change at all and will be used as references.

  1. (Optional) Add an icon on top of the Rectangle to act as a reference.

  2. Publish and resize the browser window to view the rectangle adjusting in size, and the icon maintaining its size on the canvas.

It should look like the example below

Intermediate Example – Setting up components with varying sizes AND positions

Now we’ve established the fundamental principle of the Window Size add-on working with components, we can take this one step further with other components and more advanced positioning.

We will begin with placing our components and setting their original positions:

  1. Place a metric tile on the canvas.

  2. Decide the distance that you would like to position it from the edge of the canvas (in this example, 10px will be enough)

  3. Bind the component’s X position to this value in the spreadsheet (label and highlight green as it is a static value and will not change for the first component)

  4. (Optional) Enter the Y position into the spreadsheet below the width as a fixed value (highlight green) and bind the Metric Tile’s height to this.

  5. Decide the spacing between the components (in this example, we will use 10px again to keep things easier, and we can use the same cell as the X position value)

As we will be using these numbers often, we can go ahead and place this number in a cell on the spreadsheet so we can easily reference it. I will highlight it in green as this will be a static number that doesn’t change.

  1. Enter the following formula to calculate the width of the component.

= ( canvas size – ( spacing / distance x no. spaces ) ) / no. of components

  1. The no. of components we will be placing is 4, so we can calculate the number of spaces with this by adding the two spaces at either end and the spaces in between = 5 spaces total

  2. The formula will look something like this (remember to highlight the cell yellow as this is a changing value):

  1. Now we have calculated the width, we can bind the Metric Tile to this value. As the Canvas size updates, the width of the component will adjust relative to the canvas’ width. Change the width value to give it a test, it should look something like this:

  1. Add any preferred styling to the component and then duplicate the Metric Tile 3 more times.

  2. Label 3 areas in the spreadsheet for new X positions

    1. Component 2 X

    2. Component 3 X

    3. Component 4 X

These will be the cells that we calculate the adjusting X values that will move as the canvas width changes.

With the following formulas, with each new cell, we will essentially read the matching component as the “new 0”. Let’s walk through it

  1. Use the following formulas for ‘Component 2 X’

= canvas size – ( ( width * no. of remaining components ) + ( spacing * spaces after component ) )

It should look something like this.

  1. We can now repeat this for the following X positions, reducing the ‘no. of remaining components’ and ‘spaces after component’ down by one each time.

  2. Once you have those values calculated and highlighted, go ahead and bind the duplicate components to these new values. You’ll now have something like this:

Again, we can test if these work by changing the width value in cell C3.

This was the intermediate example where we can now calculate a width value that will adapt to the updated canvas width.

It’s now time to crank it up a bit. What if we shrunk down the width to a point where it would fit a tablet or mobile screen? We don’t want to risk our components becoming squished and impossible to read:

Not want we want. The components are cramped and the text is undiscernible.

What we want to see – the components are evenly distributed, the text is visible and it is pleasing to the eye.

Expert Example – Adjusting components for various canvas sizes

As listed previously, we have ascertained various canvas sizes from Squirrel’s default sizes. In this example here, we will proceed with the preset Tablet Size (1024×768). A good place to begin is by placing this height and width in the canvas and labeling them with our static highlight color (green).

  1. Enter the new height and width of the Tablet size canvas into the yellow height and width cells to change the size of the canvas. This will allow us to see the changes being made to the components as the calculations are made.

Now that we have the new sizes, we need to begin to add conditionals to the existing formulas. These conditionals will cause major changes to occur once the canvas reaches the new sizing. We will start with resizing the widths of the components to match the new canvas size.

  1. In a new cell, label and highlight the following formula

= ( canvas width – ( spacing * no. of spaces ) ) / no. of components

It should look like this.

  1. Now that we have our width for the components went the canvas is small enough, we need to apply a conditional to our current component width formula. In cell C7, adjust the formula to the following:

= IF ( canvas width > tablet canvas width , ( canvas width – (spacing x no. of spaces ) ) / no. of components , new formula )

It should look like this.

Now we have applied the conditions to the widths of the components, you will notice that some of the components have overlapped with one another – we need to take this into account. With formulas, we can calculate the Y positions of the components that will need to be adjusted so they no longer overlap. But first, we can identify in the spreadsheet where we will put these formulas.

We will want to begin by highlighting and labeling the cells with ‘component # new X’ and ‘component # Y’. These new settings will only apply to some of the metric tiles, but we will go through that.

‘Component # new X’

This setting will only apply to the second, and fourth components we have placed as their X positions will be ones that will change as the canvas resizes.

  1. Enter the following formula into the labeled cells for the corresponding components

= canvas width – ( component width + spacing )

It should look like this.

  1. We can now apply the same conditioning to the existing X position values. With similar principles to the previous, they will look like the following:

= IF ( canvas width > new canvas width, ( = canvas size – ( ( width * no. of remaining components ) + ( spacing * spaces after component ) ), new component X cell )

It should look like this.

  1. Repeat this for the cell containing calculations for ‘component 4 X’

We can not begin to calculate the new Y positions for the third and fourth components, as these will be the ones moving as the canvas width shrinks.

  1. Label and highlight new cells for ‘component 3 Y’ and ‘component 4 Y’.

  2. Enter the following formula layout into the cells

= IF ( canvas width > tablet canvas width , y position , ( y position + spacing ) + component height )

It should look like this.

  1. We can now bind the third and fourth components to these new Y position values.

And voila! We have our components positioned as they should be. Now, you can publish and view in a browser, and resize the window to see the changes.

Using these principles, you can apply the Window Size add-on to all of your projects to have dynamically changing visuals and positions within your project.

Last updated


Copyright © 2019 - 2024 InfoSol Ltd. All rights reserved.