-
Notifications
You must be signed in to change notification settings - Fork 0
feat(block-kit): add card block example #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+86
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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")) | ||
| .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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
staticimports! I find it much more clear for focused snippets because it removes magic of where thatmarkdownTextis from!🐐 ramble: I opened adjacent PR for the action block and
elementsand 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.There was a problem hiding this comment.
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.