Mobile Usability – Custom Scrollbars

On mobile devices, Usability is everything.
Because of this, standard elements are often designed broader and larger.

Example of a 30px wide and 100px thumb custom scrollbar:

/*A custom scrollbar is configurable. 
Here, it is broader than the standard.
This way, it is easier to use on touchscreen 
or other small or mobile devices.*/

/* width */
::-webkit-scrollbar {
    width: 30px;
}

/* Track */
::-webkit-scrollbar-track {
    background: #f1f1f1;
}

/* Handle */
::-webkit-scrollbar-thumb {
    height: 100px!important;
    background: #888;
}

/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
    background: #555;
}
/*end custom scrollbar*/

Pixel sizes – Mobile – Screen – Resolution -Definition

This is a compilation from the Web. I do not know the original author. As I am good at compiling information, I think it will be useful, though.

In case you are the author of this and can prove it, please drop me a line so we can link to your content.

Standard Size Ratio Definition

16:9 Ratio
4:3 Ratio
3:2 Ratio
2:1 Ratio

Delay function in JavaScript JS

The timeout function puts a thread to sleep, which is often NOT what you want:

function someFunction(param) {
    setTimeout(function () {
        if (param == -1) {
            console.log('Parameter is -1');
        }
    }, 2000);//2 sec delay
}

Instead with ECMA6 you can use the async/await syntax.

const delay = ms => new Promise(res => setTimeout(res, ms));

const delayFunction = async () => {
  await delay(2000);
  console.log("[X] 2 sec delay.");
};

The Code looks like being synchronous while it is in fact asynchronous. This makes it easier to maintain. Plus you avoid a freeze in UIs in case you want to delay for the human eye in a UI…

React Hook Component Conditional Rendering

The HTML is rerendered after click and displays the chosen content.

We use a stateful variable.

The stateful variable (here, a boolean):

const [status, setStatus] = useState(false);

The #useEffect method to react on variable state change:

useEffect(() => {
    ...
},[status]);

The methods for the buttons:

const handleClickStatus = () => setStatus(true);
const handleClickSomething = () => setStatus(false);

The returned HTML:

if (status === true) {
    return (
        <div>
            <Button variant="contained" 
                    color="primary" 
                    onClick={handleClickSomething}>
                Primary
            </Button>
            <Button variant="contained" 
                    color="secondary" 
                    onClick={handleClickStatus}>
                Secondary
            </Button>STATUS
        </div>
    );
} else {
    return (
        <div>
            <Button variant="contained" 
                    color="primary" 
                    onClick={handleClickContent}>
                Primary
            </Button>
            <Button variant="contained" 
                    color="secondary" 
                    onClick={handleClickStatus}>
                Secondary
            </Button>CONTENT
        </div>
    );
}

Chrome – add insecure origins as secure

  1. Navigate to `chrome://flags/#unsafely-treat-insecure-origin-as-secure` in Chrome.
  2. Find and enable the `Insecure origins treated as secure` section (see below).
  3. Add any addresses you want to ignore the secure origin policy for. Remember to include the port number too (if required).
  4. Save and restart Chrome.

React – use CSS depending on states (change class according to a variable’s state)

For this example I use React Hooks and a Component with a stateful variable.
When the variable is changed, the CSS class is changed accordingly.
The part in the component is automatically re-rendered on the fly.

//The variable is called 'active'. 
//This is an example. Choose any name.
const [active, setActive] = useState(0);

We use the constant #getStyle to return a CSS class name based on the variable.

    const getStyle = (input) => {
        if (input === active){
            // This example is freely chosen. 
            //button-active is the CSS class' name
            return "button-active";
        } else {
            return "button-inactive";
        }
    };

The return statement returns rendered HTML.

//The hard coded value 0 can be any dynamic 
//variable from JS or React, like one from the 
//props (Properties) object
return (
  <div className={getStyle(0)}>
  </div>
);
  • Stateful variable called ‚active‘
  • Const named #getStyle with an input parameter
  • render method returning HTML with a css class name
  • Input is provided to the #getStyle const
  • If the variable ‚active‘ is changed by the application somewhere, the HTML is re-rendered
  • The input is compared to the variable and the name of the CSS class returned

CSS Grid Layout Example – Visible Structure – Easy Tutorial

4 Items, stretched with different sizes across a Matrix 4×4 grid.

You can see the Grid structure in the CSS in this example.

Picture:

CSS:

.item-a {
    grid-area: itemA;
    border: 1px solid violet;
}
.item-b {
    grid-area: itemB;
    border: solid 1px cyan;
}
.item-c {
    grid-area: itemC;
    border: solid 1px yellow;
}
.item-d {
    grid-area: itemD;
    border: solid 1px red;
}

.container {
    height: 100%;
    border: solid 1px green;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr 1fr;
    grid-template-areas:
            "itemA itemA itemC itemC"
            "itemA itemA itemC itemC"
            "itemA itemA itemD itemD"
            "itemA itemA itemD itemD";

HTML (in this example a React JS Grid):

return (
        <Grid className={"container"}>
            <Grid item className={"item-a"}></Grid>
            <Grid item className={"item-b"}></Grid>
            <Grid item className={"item-c"}></Grid>
            <Grid item className={"item-d"}></Grid>
        </Grid>
    );

React – stateful variables and objects (State) & Hooks & Components

Imagine you want to re-render a Component when a variable changes its state.

Your Component:

function MyComponent(props) {

Your variable, a name:

const [name, setName] = useState("OLD NAME");

A #useEffect method to react to changes of that specific variable:

useEffect(()=>{
    alert('new name!');//a small alert to show you the effect immediately
    setName('NEW NAME');
}, [name]);

…and a return with that name being rendered:

return (
    <div className={"someFancyCssClass"}>{name}</div>            
);

Note that, in this example, the initial OLD NAME is immediately replaced.
Reason for it: we set the name during initialization in the const.

Multiple Webapps with Docker Compose

  • Learn how to setup a custom network for your containers: container A talks to container B
  • Learn how to run multiple webapps inside Docker containers on different ports with Docker Compose

Download complete source code here

  • Create the network with $docker network create skynet
  • Run with $docker-compose up
  • Stop with $<Ctrl + C>
  • Clean up from time to time (beware, network will be deleted as well) with $docker system prune -a -f
  • access via http://localhost:8080/sample
  • access via http://localhost:8081/sample