Code snippets mentioned here should be added to the page Settings -> Custom Code -> Code Inside Header, unless stated otherwise.
Global variables
-
There is a
<div> element on top of the document <body> that stores application id and page id on it’s data-appid and data-pageid attributes.
-
If there is a logged in user
window.logged_in_user object is available with softr_user_email , softr_user_full_name properties. If you have synced your users table with a data source, you will also be able to access the other properties about the user as well.
-
Block specific data is stored on
window object with hrid as an identifier of that block. (ex. window['table1'] in case hrid is table1).
The window[hrid] object differs based on block type.
-
List, List details, Kanban, Chart, Organization chart, Calendar, Twitter and Map blocks have
baseId and tableName properties.
-
Form block has
airtableBaseUrl property
-
Map block has
google_map_api_key property
-
There is
openSwModal global method that opens given url in modal.
Example: openSwModal('https://softr.io/')
Charts
Chart colors
To change chart default colors we should set
where chart1 is the block hrid.
Chart invalidate
Invalidate the chart cache after 5 seconds:
where chart1 is the block hrid.
Chart reload
Reload after 5 seconds:
where chart1 is the block hrid.
Chart invalidate and reload
Invalidate the cache and reload the chart:
Where chart1 is the block hrid
Chart v5 change colors depending on value WIP
Browser Events
Adding event listeners to elements that were rendered by React is tricky.
You may add an event listener but after React re-renders the component it might loose your listener because React can (in some cases) remove and re-create element that you added the listener on.
To handle this the event listener should be added on parent element that doesn’t re-render and check the element selector after event trigger.
Example:
This way you can handle click event on table rows with table1 hrid.
Generic Custom Events
There are custom events that Softr blocks trigger or listen to.
We also add hrid to the event name to identify the block the event refers to.
block-loaded
block-loaded event is triggered when React mounts the block into DOM. It can be used instead of DOMContentLoaded event that is used in old custom codes.
ex.
get-record
get-record event is triggered on every single data response from softr data service. It is used on list-details blocks and it can be used as for getting the data and using it for other 3rd party calls or as a indicator that after some small interval time the block will be fully rendered.
ex.
get-records
get-records event is triggered on every data response from softr data service. It can be used as for getting the data and using it for other 3rd party calls or as a indicator that after some small interval time the block will be fully rendered.
ex.
Also there is an get-records:before event that is triggered before sending request, It can be used to catch the inline filter or search field changes.
update-records
update-records event is listened by all blocks that use external data.
It can be used to change/add/remove the data that should be rendered. Mostly it can be used in pair with get-records.
ex.
add-record
add-record-hrid event is fired when the form submission in the add-record modal is triggered.
add-record-success-hrid event is fired when the submission is successful
add-record-failure-hrid event is fired when the submission has failed
update-record
update-record-success event with field values is triggered after getting response with success status code.
update-record-failure event is triggered after getting response with error code.
ex.
upvote-record
upvote-record-success event with field values is triggered after getting response with success status code and datail.
upvote-record-failure event is triggered after getting response with error code.
ex.
call-api (webhook-trigger)
call-api-success event with field values is triggered after getting response with a success status code.
call-api-failure event is triggered after getting response with error code.
ex.
Blocks Custom events
Calendar block trigger reload block
ex.
List blocks trigger reload block
ex.
update-fields
update-fields event is listened by blocks that use form inputs with Formik library. Currently it’s blocks under Form and User Accounts categories. It can be used to update input values with custom code. This is used to update form values from a code automatically… let’s say after form is rendered client side custom code fetches a data from third party API and wants to set into form to be submitted… (keep in mind user attributes and URL attributes can be prefilled with default functionality without code)
ex.
submit-form event with field values as an attribute is triggered before sending form submission request.
submit-form-success event with field values is triggered after getting response with success status code.
submit-form-failure event is triggered after getting response with error code.
ex.
Make error massages of inputs in forms and user account blocks changeable/translatable via a custom code:
set-logo-link
set-logo-link event is used to set header logo link to custom url.
ex.
trigger action before logout
first will trigger custom action and after 300 ms will continue to Sign Out
ex.
block-loaded event is triggered when React mounts the block into DOM.
ex.
Tab Container Custom Events
tab-selected event is triggered every time the user selects a new tab in the Tab Container.
The event is triggered only when the active tab is changed, if the user clicks on the already active tab the event won’t be triggered.
ex.
Styling
There is still bootstrap included in the page, but it will be removed in the near future. So try not to use it.
Material-ui React component library is used in the new blocks. It adds classes to all small to large components with the prefix of Mui those classes can be used to add custom styles to the elements, but we may also change it to Softr or something similar in the near future (ex. MuiInputBase to SoftrInputBase).
Try wrapping selector with hrid to add more priority to your selector.
ex.
There are also some attributes that expose the content of the element. It can help identify and style them based on it.
-
data-content attribute on tag elements
-
data-rating attribute on rating elements
Because CSS supports attribute selectors, they can be used to style the elements based on content.
ex.
Overriding navigation styles
Kanban block custom column background colors
ex.
ex.
Selectors available for styling using custom code
Various CSS classes and data- attributes let you style UI elements and select them with JavaScript.
Class names use the .softr- prefix to distinguish them from internal classes that may change.
Similarly, stable data-* attributes use the data-softr-* prefix, with a few exceptions like data-action-id.
Let’s see how to customize the appearance of input fields in the “Add record” form.
Here’s the end result: