discourse/app/assets/stylesheets/common/components/buttons.scss
Joffrey JAFFEUX 582de0ffe3
DEV: adds blocks support to chat messages (#29782)
Blocks allow BOTS to augment the capacities of a chat message. At the moment only one block is available: `actions`, accepting only one type of element: `button`.

<img width="708" alt="Screenshot 2024-11-15 at 19 14 02" src="https://github.com/user-attachments/assets/63f32a29-05b1-4f32-9edd-8d8e1007d705">

# Usage

```ruby
Chat::CreateMessage.call(
  params: {
    message: "Welcome!",
    chat_channel_id: 2,
    blocks: [
      {
         type: "actions",
         elements: [
           { value: "foo", type: "button", text: { text: "How can I install themes?", type: "plain_text" } }
         ]
      }
    ]
  },
  guardian: Discourse.system_user.guardian
)
```

# Documentation

## Blocks

### Actions

Holds interactive elements: button.

#### Fields

| Field | Type | Description | Required? |
|--------|--------|--------|--------|
| type | string | For an actions block, type is always `actions` | Yes |
| elements | array | An array of interactive elements, maximum 10 elements | Yes |
| block_id | string | An unique identifier for the block, will be generated if not specified. It has to be unique per message | No |

#### Example

```json
{
  "type": "actions",
  "block_id": "actions_1",
  "elements": [...]
}
```

## Elements

### Button

#### Fields

| Field | Type | Description | Required? |
|--------|--------|--------|--------|
| type | string | For a button, type is always `button` | Yes |
| text | object | A text object holding the type and text. Max 75 characters | Yes |
| value | string | The value returned after the interaction has been validated. Maximum length is 2000 characters | No |
| style | string | Can be `primary` ,  `success` or `danger` | No |
| action_id | string | An unique identifier for the action, will be generated if not specified. It has to be unique per message | No |

#### Example

```json
{
  "type": "actions",
  "block_id": "actions_1",
  "elements": [
    {
      "type": "button",
      "text": {
          "type": "plain_text",
          "text": "Ok"
      },
      "value": "ok",
      "action_id": "button_1"
    }
  ]
}
```

## Interactions

When a user interactions with a button the following flow will happen:

- We send an interaction request to the server
- Server checks if the user can make this interaction
- If the user can make this interaction, the server will:

  * `DiscourseEvent.trigger(:chat_message_interaction, interaction)`
  * return a JSON document
  
  ```json
  {
    "interaction": {
        "user": {
            "id": 1,
            "username": "j.jaffeux"
        },
        "channel": {
            "id": 1,
            "title": "Staff"
        },
        "message": {
            "id": 1,
            "text": "test",
            "user_id": -1
        },
        "action": {
            "text": {
                "text": "How to install themes?",
                "type": "plain_text"
            },
            "type": "button",
            "value": "click_me_123",
            "action_id": "bf4f30b9-de99-4959-b3f5-632a6a1add04"
        }
    }
  }
  ```
  * Fire a `appEvents.trigger("chat:message_interaction", interaction)`
2024-11-19 07:07:58 +01:00

466 lines
8.7 KiB
SCSS

// --------------------------------------------------
// Buttons
// --------------------------------------------------
// Base
// --------------------------------------------------
@mixin btn(
$text-color: var(--primary),
$bg-color: var(--primary-low),
$icon-color: var(--primary-high),
$hover-text-color: var(--secondary),
$hover-bg-color: var(--primary-medium),
$hover-icon-color: var(--primary-low)
) {
@include form-item-sizing;
display: inline-flex;
align-items: center;
justify-content: center;
margin: 0;
font-weight: normal;
color: $text-color;
&:visited {
// covers cases where we add button classes to links
color: $text-color;
}
background-color: $bg-color;
background-image: linear-gradient(
to bottom,
rgba(0, 0, 0, 0),
rgba(0, 0, 0, 0)
);
border-radius: var(--d-button-border-radius);
transition: var(--d-button-transition);
cursor: pointer;
.d-icon {
color: $icon-color;
margin-right: 0.45em;
transition: var(--d-button-transition);
// For Windows High Contrast (see whcm.scss for more)
@media (forced-colors: active) {
color: ButtonText;
}
}
.d-button-label + .d-icon {
margin-left: 0.45em;
margin-right: 0;
}
&.no-text {
.d-icon {
margin-right: 0;
}
}
@include hover {
background-color: $hover-bg-color;
color: $hover-text-color;
.d-icon {
color: $hover-icon-color;
// For Windows High Contrast (see whcm.scss for more)
@media (forced-colors: active) {
color: Highlight;
}
}
}
&:focus {
outline: none;
background-color: $hover-bg-color;
color: $hover-text-color;
.d-icon {
color: $hover-icon-color;
@media (forced-colors: active) {
color: Highlight;
}
}
&:focus-visible {
@include darken-background($hover-bg-color, 0.1);
}
}
.discourse-no-touch &:active:not(:hover):not(:focus),
.discourse-no-touch &.btn-active:not(:hover):not(:focus),
&:active:not(:hover):not(:focus),
&.btn-active:not(:hover):not(:focus) {
@include darken-background($bg-color, 0.6);
color: $hover-text-color;
.d-icon {
color: $hover-icon-color;
}
}
.discourse-no-touch &:active,
.discourse-no-touch &.btn-active,
&:active,
&.btn-active {
@include darken-background($bg-color, 0.3);
color: $hover-text-color;
.d-icon {
color: $hover-icon-color;
}
}
&[disabled],
&.disabled {
&:not(.is-loading) {
opacity: 0.4;
}
&:hover {
color: $text-color;
background: $bg-color;
.d-icon {
color: $icon-color;
}
}
cursor: not-allowed;
}
.loading-container {
display: none;
margin: 0 6.75px 0 0;
}
&.is-loading {
&.btn-text {
&.btn-small {
.loading-icon {
font-size: var(--font-down-1);
margin-right: 0.2em;
}
}
}
.loading-icon {
animation: rotate-forever 1s infinite linear, fadein 1s;
}
}
}
.btn.hidden {
display: none;
}
// Default button
// --------------------------------------------------
.btn {
@include btn;
}
// Primary button
// --------------------------------------------------
.btn-primary {
@include btn(
$text-color: var(--secondary),
$bg-color: var(--tertiary),
$icon-color: var(--secondary),
$hover-bg-color: var(--tertiary-hover),
$hover-icon-color: var(--secondary)
);
}
// Danger button
// --------------------------------------------------
.btn-danger {
@include btn(
$text-color: var(--secondary),
$bg-color: var(--danger),
$icon-color: var(--danger-low),
$hover-bg-color: var(--danger-hover),
$hover-icon-color: var(--danger-low)
);
}
// ✘ and ✔ buttons
// --------------------------------------------------
.btn.cancel {
@include btn(
$text-color: var(--secondary),
$bg-color: var(--danger),
$icon-color: var(--secondary),
$hover-bg-color: var(--danger-hover),
$hover-icon-color: var(--secondary)
);
}
.btn.ok {
@include btn(
$text-color: var(--secondary),
$bg-color: var(--success),
$icon-color: var(--secondary),
$hover-bg-color: var(--success-hover),
$hover-icon-color: var(--secondary)
);
}
// Social buttons
// --------------------------------------------------
.btn-social {
color: #000;
background: #fff;
border: 1px solid transparent;
border-radius: var(--d-border-radius);
&:hover,
&:focus {
box-shadow: var(--shadow-card);
outline: 1px solid #000;
}
&[href] {
color: var(--secondary);
}
&:before {
margin-right: 9px;
font-size: var(--font-0);
}
.d-icon,
&.btn:hover .d-icon {
color: #000;
}
&.google_oauth2 {
background: var(--google);
color: #333;
// non-FA SVG icon for Google in login-buttons.hbs
.d-icon {
opacity: 0.9;
}
&:hover,
&:focus {
color: inherit;
}
}
&.cas {
.d-icon {
color: var(--cas);
}
&:hover {
.d-icon {
color: var(--cas);
}
}
}
&.twitter {
.d-icon {
color: var(--twitter);
}
&:hover {
.d-icon {
color: var(--twitter);
}
}
}
&.github {
.d-icon {
color: var(--github);
}
&:hover {
.d-icon {
color: var(--github);
}
}
}
&.discord {
.d-icon {
color: var(--discord);
}
&:hover {
.d-icon {
color: var(--discord);
}
}
}
// https://developers.facebook.com/docs/facebook-login/userexperience/#buttondesign
// if you are unable to use Facebook blue, revert to black and white.
}
// Button Sizes
// --------------------------------------------------
// Small
.btn-small {
font-size: var(--font-down-1);
min-height: 20px;
}
// Large
.btn-large {
font-size: var(--font-up-1);
}
// Bonus Buttons
// --------------------------------------------------
.btn-flat {
background: transparent;
border: 0;
line-height: var(--line-height-small);
transition: var(--d-button-transition);
.d-icon {
color: var(--primary-low-mid);
transition: var(--d-button-transition);
}
.discourse-no-touch & {
&:hover,
&:focus {
color: var(--primary);
.d-icon {
color: var(--primary);
}
}
&:hover {
background: transparent;
}
&:focus {
background: var(--primary-low);
}
}
&.close {
background: transparent;
font-size: var(--font-up-2);
.d-icon {
color: var(--primary-high);
}
.discourse-no-touch & {
&:hover,
&:focus {
background: transparent;
.d-icon {
color: var(--primary);
}
}
}
&:focus {
background: transparent;
.d-icon {
color: var(--primary);
}
}
}
&.btn-text {
color: var(--tertiary);
&[disabled] {
&,
&:hover,
&.btn-hover,
&:focus {
color: var(--primary);
}
}
&:not([disabled]) {
&:hover,
&.btn-hover,
&:focus {
color: var(--tertiary-hover);
}
&:active,
&.btn-active {
@include darken-background(transparent, 0.2);
}
}
}
&:focus {
outline: none;
background: var(--primary-low);
.d-icon {
color: var(--primary);
}
}
&.back-button {
margin-bottom: 1em;
}
}
.btn-link {
background: transparent;
border: 0;
padding: 0;
color: var(--tertiary);
.discourse-no-touch & {
&:hover {
color: var(--tertiary);
background: transparent;
}
}
&:focus {
color: var(--tertiary);
background: transparent;
}
&:focus-visible {
color: var(--tertiary);
background: transparent;
@include default-focus;
}
}
@mixin btn-colors($btn-color) {
color: var(--#{$btn-color});
.d-icon {
color: inherit;
}
&:focus,
&:focus-visible {
color: var(--#{$btn-color}-hover);
}
.discourse-no-touch & {
&:hover {
color: var(--#{$btn-color}-hover);
}
}
}
.btn-transparent {
&,
&.btn-default {
background: transparent;
border: 0;
color: var(--primary);
.d-icon {
color: var(--primary-high);
}
&:focus,
&:focus-visible {
background: transparent;
color: var(--tertiary-hover);
.d-icon {
color: inherit;
}
}
.discourse-no-touch & {
&:hover {
background: transparent;
color: var(--tertiary-hover);
.d-icon {
color: inherit;
}
}
}
}
&.btn-primary {
@include btn-colors("tertiary");
}
&.btn-danger {
@include btn-colors("danger");
}
&.btn-success {
@include btn-colors("success");
}
}
.btn-mini-toggle {
border-radius: var(--d-border-radius);
padding: 0.4em 0.467em;
.d-icon {
color: var(--primary-medium);
}
@include hover {
.d-icon {
color: var(--primary);
}
}
}