Tailwind CSS Layout Splitter Plugin API
Headless, unstyled layout splitter plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Layout Splitter plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/layout-splittervia npmTerminalnpm i @preline/layout-splitter -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/layout-splitter */ /* [!code highlight:3] */ @source "../node_modules/@preline/layout-splitter/*.js"; @import "./node_modules/@preline/layout-splitter/variants.css"; @import "./node_modules/@preline/layout-splitter/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
JavaScript (auto)import "@preline/layout-splitter";JavaScript (non-auto)import HSLayoutSplitter from "@preline/layout-splitter/non-auto"; HSLayoutSplitter.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#layout-splitter"); if (el) new HSLayoutSplitter(el);
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div data-hs-layout-splitter='{
"horizontalSplitterTemplate": "<div><span class=\"absolute top-1/2 inset-s-1/2 -translate-x-2/4 -translate-y-2/4 block w-[11px] h-5 bg-stone-400\"></span></div>",
"horizontalSplitterClasses": "relative flex border-s border-stone-200"
}'>
<div class="flex h-50" data-hs-layout-splitter-horizontal-group>
<div class="h-full" data-hs-layout-splitter-item="48.2">Horizontal Block 1</div>
<div class="h-full" data-hs-layout-splitter-item="30.0">Horizontal Block 2</div>
</div>
</div>
Methods
The HSLayoutSplitter object is contained within the global window object
Set different size on window resize.
let layoutSplitter = HSLayoutSplitter.getInstance('#layout-splitter', true);
const updateLayoutSplitter = (splitter) => {
splitter.updateFlexValues([
{
id: "section-01",
breakpoints: {
"0": 0,
"1280": 15.0
}
},
{
id: "section-02",
breakpoints: {
"0": 0,
"1024": 25.4,
"1280": 21.0
}
},
{
id: "section-03",
breakpoints: {
"0": 100,
"1024": 74.6,
"1280": 64.0
}
}
]);
};
window.addEventListener('resize', () => {
if (layoutSplitter) updateLayoutSplitter(layoutSplitter.element);
});
Get instance by ID.
const layoutSplitter = new HSLayoutSplitter(document.querySelector('#layout-splitter'));
console.log("Instance:", layoutSplitter);
Events
Adds classes that can be useful when you need to minimize the sidebar.
const { element } = HSLayoutSplitter.getInstance('#layout-splitter', true);
element.on('onPrevLimit', (prev) => {
prev.classList.add('prev-limit-reached');
});
element.on('onPrevPreLimit', (prev) => {
prev.classList.add('prev-pre-limit-reached');
});
element.on('drag', ({ prev, previousFlexSize }) => {
if (previousFlexSize > 10) {
prev.classList.remove('prev-limit-reached');
}
if (previousFlexSize > 20) {
prev.classList.remove('prev-pre-limit-reached');
}
});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Layout Splitter
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Layout Splitter examples