Skip to main content

JavaScript

Most of FlatifyCSS is plain CSS, the JavaScript file is only needed for components that open, close or switch: accordions, alerts, dropdowns, popovers, modals, tabs, the show password button and text truncation. There is nothing to call, you add the classes and attributes in your HTML and the script does the rest.

index.html
<!-- Popper is needed for dropdowns and popovers -->
<script src="https://unpkg.com/@popperjs/core@2" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/flatifycss@2/dist/js/flatify-min.js" crossorigin="anonymous"></script>
Load it before the page is ready

Every feature is set up on the DOMContentLoaded event, so add the script in the <head> or anywhere in the <body>, but not with async or after the page has loaded. Clicks are listened on the document, so elements you add later with JavaScript work too, except the show password button which is bound to the .input-wrapper elements that exist when the page loads.

Classes and attributes​

Here is everything the script looks for in your HTML. Each component page explains them with examples.

Accordion​

Class or attributeWhereWhat it does
.accordion-toggleButton inside .accordion-headerOpens or closes its .accordion-item
.expendable.accordionLets more than one item stay open
.active.accordion-itemAdded when the item is open
.accordion-will-be-hidden.accordion-collapseAdded while the hide animation is running

Accordion page

Alert​

Class or attributeWhereWhat it does
.close-buttonButton inside .alertPlays the hide animation and removes the alert
.alert-will-be-removed.alertAdded while the hide animation is running

Alert page

Class or attributeWhereWhat it does
.dropdown-toggleButton inside .dropdown-wrapperOpens or closes the .dropdown next to it
.close-dropdownButton inside .dropdownCloses the dropdown
data-dropdown-direction.dropdownWhere it opens, any Popper placement like top, left-start or bottom-end. Default is bottom
data-dropdown-auto-close.dropdowntrue (default) closes on any click, inside only on clicks inside it, outside only on clicks outside it, false never
.show.dropdownAdded when it is open
.dropdown-will-be-hidden.dropdownAdded while the hide animation is running

Open dropdowns also close with the Escape key. Dropdown page, popover page

Class or attributeWhereWhat it does
.open-modalAny buttonOpens the modal set in data-modal-target
.close-modalAny buttonCloses the modal set in data-modal-target, or the modal it is inside
data-modal-target.open-modal, .close-modalCSS selector of the modal, like #my-modal
.required.modalThe modal does not close by clicking the backdrop or pressing Escape
.modal-backdropCreated automatically, clicking it closes the open modals
.modal-will-be-hidden.modalAdded while the hide animation is running
.backdrop-will-be-removed.modal-backdropAdded while the backdrop fades out

Modal page

Tabs​

Class or attributeWhereWhat it does
.tab-buttonButton inside .tabs-headerShows its tab panel
data-tab-target.tab-buttonCSS selector of the panel, if not set aria-controls is used
.active.tab-buttonAdded to the current tab button
.show.tab-panelAdded to the visible panel
.slide-left, .slide-right.tab-panelAdded based on the direction you move between tabs
.tab-will-be-hidden.tab-panelAdded while the hide animation is running

Tabs page

Forms and text​

Class or attributeWhereWhat it does
.show-password-buttonButton inside .input-wrapperShows or hides the password
.visible-password.input-wrapperAdded while the password is visible
.toggle-truncateAny elementShows or hides the full text of .truncate elements
data-truncation-target.toggle-truncateCSS selector of the text to toggle

Inputs page, text truncation page

Configuration​

There is a config.json file inside the /js folder. It is bundled into dist/js/flatify.js when you build, so change it and run gulp, read the development page for that.

OptionDefaultWhat it does
name"FlatifyCSS"Prefix of the messages in the console
enableLoggingtrueShows errors in the console when something is missing, like a modal target that does not exist
dropdownOffset[0, 20][skidding, distance] of dropdowns from their button, in pixels
navDropdownOffset[0, 0]The same for dropdowns inside a navigation bar
popoverOffset[0, 10]The same for popovers