Skip to content

Commit 4a0b430

Browse files
authored
ButtonLink: Allow to disable the button (#284)
2 parents 75e5f54 + 3340a28 commit 4a0b430

5 files changed

Lines changed: 81 additions & 2 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.action-link {
2+
color: var(--control-color, @control-color);
3+
}
4+
5+
.button-link {
6+
.action-link();
7+
.rounded-corners(3px);
8+
9+
background: var(--default-input-bg, @default-input-bg);
10+
display: inline-block;
11+
padding: 0.25em 0.5em;
12+
13+
&:hover {
14+
background: var(--default-input-hover-bg, @default-input-hover-bg);
15+
text-decoration: none;
16+
}
17+
18+
&[aria-disabled="true"] {
19+
background: none;
20+
border: 1px solid var(--control-disabled-color, @control-disabled-color);
21+
color: var(--control-disabled-color, @control-disabled-color);
22+
cursor: not-allowed;
23+
24+
.user-select(none);
25+
}
26+
}

asset/css/compat.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
}
77
}
88

9+
// Icinga Web < 2.13
10+
.button-link[aria-disabled="true"]:hover {
11+
background: none;
12+
}
13+
914
form.icinga-form {
1015
.uploaded-files {
1116
flex: 1 1 auto;

asset/css/mixin/mixins.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@
3939
.monospace-font() {
4040
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
4141
}
42+
43+
.user-select(@user-select) {
44+
-webkit-user-select: @user-select;
45+
user-select: @user-select;
46+
}

asset/css/variables.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@default-text-color-light: fade(@default-text-color, 75%);
4242
@default-text-color-inverted: @default-bg;
4343
@default-input-bg: #404d72;
44+
@default-input-hover-bg: #434374;
4445
@default-remove-bg: @state-critical;
4546
@default-remove-color: @default-text-color-inverted;
4647
@default-delete-bg: @state-critical;
@@ -150,6 +151,7 @@
150151
--default-text-color-light: fade(#535353, 75%); // --default-text-color
151152
--default-text-color-inverted: #F5F9FA;
152153
--default-input-bg: #DEECF1;
154+
--default-input-hover-bg: #C0CCCD;
153155
--default-remove-bg: var(--base-remove-bg);
154156
--default-remove-color: var(--default-text-color-inverted);
155157
--default-delete-bg: var(--base-remove-bg);

src/Widget/ButtonLink.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,54 @@
22

33
namespace ipl\Web\Widget;
44

5+
use ipl\Html\Attribute;
6+
57
/**
68
* Button like link generally pointing to CRUD actions
79
*/
810
class ButtonLink extends ActionLink
911
{
1012
protected $defaultAttributes = [
11-
'class' => 'button-link',
12-
'data-base-target' => '_main'
13+
'role' => 'button',
14+
'class' => 'button-link',
15+
'data-base-target' => '_main'
1316
];
17+
18+
/** @var ?string The explanation why the button is disabled */
19+
protected ?string $disabledExplanation = null;
20+
21+
/**
22+
* Disable the button with explanation
23+
*
24+
* @param string $explanation Why the button is disabled
25+
*
26+
* @return $this
27+
*/
28+
public function disable(string $explanation): self
29+
{
30+
$this->disabledExplanation = $explanation;
31+
32+
return $this;
33+
}
34+
35+
public function createHrefAttribute(): ?Attribute
36+
{
37+
if ($this->disabledExplanation) {
38+
return null;
39+
}
40+
41+
return parent::createHrefAttribute();
42+
}
43+
44+
public function assemble(): void
45+
{
46+
parent::assemble();
47+
48+
if ($this->disabledExplanation) {
49+
$this->addAttributes([
50+
'aria-disabled' => 'true',
51+
'title' => $this->disabledExplanation,
52+
]);
53+
}
54+
}
1455
}

0 commit comments

Comments
 (0)