Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions block-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Read the [docs](https://docs.slack.dev/block-kit/) to learn concepts behind thes
### Blocks

- **[Actions](https://docs.slack.dev/reference/block-kit/blocks/actions-block)**: Holds multiple interactive elements. [Implementation](./src/main/java/blocks/Actions.java).
- **[Card](https://docs.slack.dev/reference/block-kit/blocks/card-block)**: Displays content in a card. [Implementation](./src/main/java/blocks/Card.java).
- **[Context](https://docs.slack.dev/reference/block-kit/blocks/context-block)**: Provides contextual info, which can include both images and text. [Implementation](./src/main/java/blocks/Context.java).
- **[Divider](https://docs.slack.dev/reference/block-kit/blocks/divider-block)**: Visually separates pieces of info inside of a message. [Implementation](./src/main/java/blocks/Divider.java).
- **[File](https://docs.slack.dev/reference/block-kit/blocks/file-block)**: Displays info about remote files. [Implementation](./src/main/java/blocks/File.java).
Expand Down
30 changes: 30 additions & 0 deletions block-kit/src/main/java/blocks/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package blocks;

import com.slack.api.model.block.Blocks;
import com.slack.api.model.block.CardBlock;
import com.slack.api.model.block.composition.BlockCompositions;
import com.slack.api.model.block.element.BlockElements;
import java.util.List;

/**
* Displays content in a card.
* {@link https://docs.slack.dev/reference/block-kit/blocks/card-block/}
*/
public class Card {
/**
* A card with icon, title, subtitle, hero image, body, and an action button.
*/
public static CardBlock example01() {
CardBlock block = Blocks.card(c -> c.icon(BlockElements.imageElement(
i -> i.imageUrl("https://picsum.photos/36/36").altText("Icon")))
.title(BlockCompositions.markdownText("Lumon Industries"))
.subtitle(BlockCompositions.markdownText("Committed to work-life balance"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏆 praise: Thanks for inlining this instead of using static imports! I find it much more clear for focused snippets because it removes magic of where that markdownText is from!

🐐 ramble: I opened adjacent PR for the action block and elements and am including similar for composition objects also. The "static" import pattern is not so familiar to me but I understand it's common to use in development.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 note: I have follow up planned to mirror this in other examples with
#74 and #75 and I notice it's in #71 too bless.

.heroImage(BlockElements.imageElement(
i -> i.imageUrl("https://picsum.photos/400/300").altText("Sample hero image")))
.body(BlockCompositions.markdownText("Please enjoy each card equally."))
.actions(List.of(BlockElements.button(b -> b.text(BlockCompositions.plainText(
pt -> pt.text("Action Button").emoji(false)))
.actionId("button_action")))));
return block;
}
}
55 changes: 55 additions & 0 deletions block-kit/src/test/java/blocks/CardTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package blocks;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.google.gson.JsonParser;
import com.slack.api.model.block.CardBlock;
import com.slack.api.util.json.GsonFactory;
import org.junit.jupiter.api.Test;

public class CardTest {
@Test
public void testExample01() {
CardBlock block = Card.example01();
String actual = GsonFactory.createSnakeCase().toJson(block);
String expected = """
{
"type": "card",
"icon": {
"type": "image",
"image_url": "https://picsum.photos/36/36",
"alt_text": "Icon"
},
"title": {
"type": "mrkdwn",
"text": "Lumon Industries"
},
"subtitle": {
"type": "mrkdwn",
"text": "Committed to work-life balance"
},
"hero_image": {
"type": "image",
"image_url": "https://picsum.photos/400/300",
"alt_text": "Sample hero image"
},
"body": {
"type": "mrkdwn",
"text": "Please enjoy each card equally."
},
"actions": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Action Button",
"emoji": false
},
"action_id": "button_action"
}
]
}
""";
assertEquals(JsonParser.parseString(expected), JsonParser.parseString(actual));
}
}