Skip to content

Commit 779cff5

Browse files
authored
Add accessibility_label to button block element (#1167)
1 parent 555c891 commit 779cff5

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

slack_sdk/models/blocks/block_elements.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ class ButtonElement(InteractiveElement):
233233

234234
@property
235235
def attributes(self) -> Set[str]:
236-
return super().attributes.union({"text", "url", "value", "style", "confirm"})
236+
return super().attributes.union(
237+
{"text", "url", "value", "style", "confirm", "accessibility_label"}
238+
)
237239

238240
def __init__(
239241
self,
@@ -244,6 +246,7 @@ def __init__(
244246
value: Optional[str] = None,
245247
style: Optional[str] = None, # primary, danger
246248
confirm: Optional[Union[dict, ConfirmObject]] = None,
249+
accessibility_label: Optional[str] = None,
247250
**others: dict,
248251
):
249252
"""An interactive element that inserts a button. The button can be a trigger for
@@ -271,6 +274,9 @@ def __init__(
271274
Use "danger" even more sparingly than "primary".
272275
If you don't include this field, the default button style will be used.
273276
confirm: A confirm object that defines an optional confirmation dialog after the button is clicked.
277+
accessibility_label: A label for longer descriptive text about a button element.
278+
This label will be read out by screen readers instead of the button text object.
279+
Maximum length for this field is 75 characters.
274280
"""
275281
super().__init__(action_id=action_id, type=self.type)
276282
show_unknown_key_warning(self, others)
@@ -281,6 +287,7 @@ def __init__(
281287
self.value = value
282288
self.style = style
283289
self.confirm = ConfirmObject.parse(confirm)
290+
self.accessibility_label = accessibility_label
284291

285292
@JsonValidator(f"text attribute cannot exceed {text_max_length} characters")
286293
def _validate_text_length(self) -> bool:
@@ -302,6 +309,15 @@ def _validate_value_length(self) -> bool:
302309
def _validate_style_valid(self):
303310
return self.style is None or self.style in ButtonStyles
304311

312+
@JsonValidator(
313+
f"accessibility_label attribute cannot exceed {text_max_length} characters"
314+
)
315+
def _validate_accessibility_label_length(self) -> bool:
316+
return (
317+
self.accessibility_label is None
318+
or len(self.accessibility_label) <= self.text_max_length
319+
)
320+
305321

306322
class LinkButtonElement(ButtonElement):
307323
def __init__(

tests/slack_sdk/models/test_elements.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ def test_document_3(self):
102102
self.assertDictEqual(input, ButtonElement(**input).to_dict())
103103
self.assertDictEqual(input, LinkButtonElement(**input).to_dict())
104104

105+
def test_document_4(self):
106+
input = {
107+
"type": "button",
108+
"text": {"type": "plain_text", "text": "Save"},
109+
"style": "primary",
110+
"value": "click_me_123",
111+
"action_id": "button",
112+
"accessibility_label": "This label will be read out by screen readers",
113+
}
114+
self.assertDictEqual(input, ButtonElement(**input).to_dict())
115+
105116
def test_json(self):
106117
self.assertDictEqual(
107118
{
@@ -158,6 +169,15 @@ def test_invalid_style(self):
158169
text="Button", action_id="button", value="button", style="invalid"
159170
).to_dict()
160171

172+
def test_accessibility_label_length(self):
173+
with self.assertRaises(SlackObjectFormationError):
174+
ButtonElement(
175+
text="Hi there!",
176+
action_id="button",
177+
value="click_me",
178+
accessibility_label=("1234567890" * 8),
179+
).to_dict()
180+
161181

162182
class LinkButtonElementTests(unittest.TestCase):
163183
def test_json(self):

0 commit comments

Comments
 (0)