Maintainable code for Slack interactive messages, modals, and home tabs.
Lightweight, zero-dependency JavaScript library for Slack Block Kit UI.
View the Docs »
Quick Start Guide
·
Request Feature
·
Report Bug
Block Builder helps you keep your Slack app code for UI maintainable, testable, and reusable. It has a simple builder syntax inspired by SwiftUI and lets you code the way you want to code.
:zap: Features
- Simple SwiftUI inspired syntax.
- In-depth doc site at https://blockbuilder.dev.
- Support for all current Slack Block Kit objects – Surfaces, Blocks, Elements, and Composition Objects (View Support).
- Super helpful JSDoc hints that include real-world explanations, Slack validation rules, and a direct link to the object's documentation on Slack's API doc site.
- Output of the composed UI as either an object, JSON string, or array of blocks.
- A
printPreviewURL()
method that outputs to the console a link to preview your UI on Slack's Block Kit Builder website. - Small size, with zero dependencies.
:rocket: Coming Soon
- TypeScript type definitions.
- Components, such as an Accordion module.
- Configurable option to check Slack validation rules.
- Guide for Slack apps with tips, tricks, and best practices.
:gift: Benefits
- Write three times less code.
- Build more sophistocated, elegant flows.
- Design better UI architecture (works as a template engine).
- Focus more on experience and code in your IDE than on reading Slack API docs.
- Code the way you want to code – not forced into any single paradigm.
- Easily integrate localizations into your app.
:phone: Let's Talk?
Feedback – love it! Aside from GitHub Issues, there are Slack channels available in popular bot communities to discuss Block Builder – we'll see you there! :raised_hands:
:floppy_disk: Installation
Using NPM:
npm install --save slack-block-builder
Using Yarn:
yarn add slack-block-builder
:space_invader: Usage
For full documentation, make sure you head over to https://blockbuilder.dev.
Importing
At the top level of the library, there are a few objects exposed for import. You'll be using these to build out your UI:
import { Message, Blocks, Elements, Bits } from 'slack-block-builder';
You can also import, in place of Message
, Modal
or HomeTab
.
Exposed Imports
Modal
– Used to create a modal surface.
Message
– Used to create a message surface.
HomeTab
– Used to create a message surface.
Note that since you'll more often only be working with one surface per file, they are exposed individually, whereas the rest of the objects are grouped into categories.
Blocks
– Layout blocks used to organize the UI.
Elements
– UI elements that are used to capture user interaction.
Bits
– These are composition objects from Slack's docs that are more focused on UI, not data structure (unlike text and filter objects). Included are Options
, OptionGroup
, and ConfirmationDialog
. They felt like they were deserving of their own category.
Object Support and Reference
Below is a list of supported objects and how to access them in Block Builder:
Name | Type | Support | Accessed Via |
---|
Home Tab | Surface | :white_check_mark: | HomeTab() |
Message | Surface | :white_check_mark: | Message() |
Modal | Surface | :white_check_mark: | Modal() |
Actions | Block | :white_check_mark: | Blocks.Actions() |
Context | Block | :white_check_mark: | Blocks.Context() |
Divider | Block | :white_check_mark: | Blocks.Divider() |
File | Block | :white_check_mark: | Blocks.File() |
Image | Block | :white_check_mark: | Blocks.Image() |
Input | Block | :white_check_mark: | Blocks.Input() |
Section | Block | :white_check_mark: | Blocks.Section() |
Button | Element | :white_check_mark:️ | Elements.Button() |
Checkboxes | Element | :white_check_mark: | Elements.Checkboxes() |
Date Picker | Element | :white_check_mark: | Elements.DatePicker() |
Image | Element | :white_check_mark: | Elements.Img() |
Overflow Menu | Element | :white_check_mark: | Elements.OverflowMenu() |
Radio Buttons | Element | :white_check_mark: | Elements.RadioButtons() |
Plain-Text Input | Element | :white_check_mark: | Elements.TextInput() |
Select Menus | Element | :white_check_mark: | Elements.[Type]Select() |
Multi-Select Menus | Element | :white_check_mark: | Elements.[Type]MultiSelect() |
Option | Composition Object | :white_check_mark: | Bits.Option() |
Confirm Dialog | Composition Object | :white_check_mark: | Bits.ConfirmationDialog() |
Option Group | Composition Object | :white_check_mark: | Bits.OptionGroup() |
Creating a Simple Interactive Message
Let's take a look at how to compose an interactive message. Even though Slack now has modals, these have always been the basis for Slack apps.
We can create a piece of UI using only the setter methods:
import { Message, Blocks, Elements } from 'slack-block-builder';
const myMessage = ({ channel }) => {
return Message()
.channel(channel)
.text('Alas, my friend.')
.blocks(
Blocks.Section()
.text('One does not simply walk into Slack and click a button.'),
Blocks.Section()
.text('At least that\'s what my friend Slackomir said :crossed_swords:'),
Blocks.Divider(),
Blocks.Actions()
.elements(
Elements.Button()
.text('Sure One Does')
.actionId('gotClicked')
.danger(),
Elements.Button()
.text('One Does Not')
.actionId('scaredyCat')
.primary()))
.asUser()
.buildToJSON();
};
Alternatively (and preferably), we can combine both the setter methods and the params to shorten it:
import { Message, Blocks, Elements } from 'slack-block-builder';
const myShorterMessage = ({ channel }) => {
return Message({ channel, text: 'Alas, my friend.' })
.blocks(
Blocks.Section({ text: 'One does not simply walk into Slack and click a button.' }),
Blocks.Section({ text: 'At least that\'s what my friend Slackomir said :crossed_swords:' }),
Blocks.Divider(),
Blocks.Actions()
.elements(
Elements.Button({ text: 'Sure One Does', actionId: 'gotClicked' })
.danger(),
Elements.Button({ text: 'One Does Not', actionId: 'scaredyCat' })
.primary()))
.asUser()
.buildToJSON();
};
Both of these examples render the message below. And the best part? It only took 15 lines of code, as opposed to the 44 lines of JSON generated as a result.
View Example on Slack Block Kit Builder Website
Creating a Simple Modal
Let's take a look at how modals are created.
Here we'll also take a look at working with Bits. You'll see in this example that we're hardcoding the options in the select menu. There are, of course, better ways to handle that, by using the Array.map()
method, but they are listed here separately to demonstrate the usage.
First, an example using just our setter methods:
import { Modal, Blocks, Elements, Bits } from 'slack-block-builder';
const myModal = () => {
return Modal()
.title('PizzaMate')
.blocks(
Blocks.Section()
.text('Hey there, colleague!'),
Blocks.Section()
.text('Hurray for corporate pizza! Let\'s get you fed and happy :pizza:'),
Blocks.Input()
.label('What can we call you?')
.element(
Elements.TextInput()
.placeholder('Hi, my name is... (What?!) (Who?!)')
.actionId('name')),
Blocks.Input()
.label('Which floor are you on?')
.element(
Elements.TextInput()
.placeholder('HQ – Fifth Floor')
.actionId('floor')),
Blocks.Input()
.label('What\'ll you have?')
.element(
Elements.StaticSelect()
.placeholder('Choose your favorite...')
.actionId('item')
.options(
Bits.Option().text(':cheese_wedge: With Cheeze').value('012'),
Bits.Option().text(':fish: With Anchovies').value('013'),
Bits.Option().text(':cookie: With Scooby Snacks').value('014'),
Bits.Option().text(':beer: I Prefer Steak and Beer').value('015'))))
.submit('Get Fed')
.buildToJSON();
};
Alternatively (and preferably), we can combine both the setter methods and the params to shorten it:
import { Modal, Blocks, Elements, Bits } from 'slack-block-builder';
const myShorterModal = () => {
return Modal({ title: 'PizzaMate', submit: 'Get Fed' })
.blocks(
Blocks.Section({ text: 'Hey there, colleague!' }),
Blocks.Section({ text: 'Hurray for corporate pizza! Let\'s get you fed and happy :pizza:' }),
Blocks.Input({ label: 'What can we call you?' })
.element(
Elements.TextInput({ placeholder: 'Hi, my name is... (What?!) (Who?!)' })
.actionId('name')),
Blocks.Input({ label: 'Which floor are you on?' })
.element(
Elements.TextInput({ placeholder: 'HQ – Fifth Floor' })
.actionId('floor')),
Blocks.Input({ label: 'What\'ll you have?' })
.element(
Elements.StaticSelect({ placeholder: 'Choose your favorite...' })
.actionId('item')
.options(
Bits.Option({ text: ':cheese_wedge: With Cheeze', value: '012' }),
Bits.Option({ text: ':fish: With Anchovies', value: '013' }),
Bits.Option({ text: ':cookie: With Scooby Snacks', value: '014' }),
Bits.Option({ text: ':beer: I Prefer Steak and Beer', value: '015' }))))
.buildToJSON();
};
Both of these examples render the modal below.
View Example on Slack Block Kit Builder Website
:link: Other Useful Slack-Related Projects
Bolt for JavaScript – A simple framework for building Slack apps, developed by Slack themselves.
Node Slack SDK – A great and powerful SDK for building Slack Apps from the ground up.
JSX-Slack – Awesome way to create Slack Block Kit UIs using JSX.
:fire: Acknowledgements
Taras Neporozhniy (@bravecow) - For mentorship over the years!
Alexey Chernyshov (@ft502 on Dribbble) - For such a beautiful logo!
SlackHQ (@slackhq) - For such a wonderful product and API!
:black_nib: Author
Ray East (@raycharius) - Huge Fan of Slack and Block Builder Maintainer