The previous chapter of the book of dark showed a quick & dirty way to store data in a web pages’s HTML. In this chapter I’ll write about showing and hiding pages and page elements based on state changes in the single page application (SPA) by using CSS rules.
- The page HTML describes in the DOM all forms and fields (elements, for brevity) that would ever be necessary
- The elements that are visible depending on application state (SDE for selective disclosure elements, in short) are hidden with CSS by default
- Application state is reflected by one or more top-level CSS classs (e.g. on the <form> element or even the <body> element) which unveil their respective SDEs.
Let’s see how this works in detail. Let’s try to model an address book SPA with two screens: an overview of the stored address entries and a detail view.
The (single) HTML page contains markup for both screens and some CSS that hides them on condition:
<form class="hidden show-on-overview">
<a href="#details_1">Alice Crypter</a>
<a href="#details_2">Bob Crypter</a>
<a href="#details_3">John Doe</a>
<a href="#details_4">Dr. Oz</a>
<form class="hidden show-on-details">
<label>Name <input id=name type=text/></label>
<label>Street <input id=street type=text/></label>
<label>Postal <input id=postal type=text/></label>
The interesting CSS for this page is:
Per default, both the overview and details screens are hidden and the page looks blank. The overview screen becomes visible by adding the “state-overview” class to container <div>. Likewise a transition to the details screen involves:
- remove the state-overview class from the container, which hides the overview screen
- add the state-details class to the container, which shows the details screen
More advanced functionality, like showing an edit button depending on roles, would work similarly:
<body class="role-editor ...">
<button onclick="editField('name')" class="hidden needs-role-editor">Edit</button>
The best way to keep track of state is by using business logic events. Whenever a relevant action happens, an event is fired which can be consumed by code that modifies the UI, cleanly decoupling logic from view.
The address book app has two states which are modelled by URL hashes, #overview and #details_ID (where ID depends on the entry inspected). The events are implicitly fired by clicking on <a href=”#hash”> links and are processed by listening to them. A jquery implementation would look like this:
let token = window.location.hash.substring(1, window.location.hash);
if (token == "overview") $(window).trigger("overview");
$.ajax(....) //load data into the details page
A component view of the same constellation: a controller consumes events (browser and domain) and assigns CSS classes to a container document which reflect the application state:
The SPA discussed can be played with over here: https://ggeorgovassilis.github.io/public/blog/dark-arts-selective-view.html