@@ -39,14 +39,17 @@ export default class HTMLCodeBlockElement extends HTMLElement {
39
39
return endgine . highlightAuto ( src ) ;
40
40
}
41
41
42
-
43
42
#shadowRoot: ShadowRoot ;
44
43
#codeBlock: HTMLElement ;
45
44
#codeWrap: HTMLPreElement ;
46
45
/** Actual value of the accessor `value` */
47
46
#value: string = '' ;
47
+ /** Actual value of the accessor `label` */
48
+ #label: string = '' ;
48
49
/** Actual value of the accessor `language` */
49
50
#language: string = '' ;
51
+ /** Actual value of the accessor `controls` */
52
+ #controls: boolean = false ;
50
53
51
54
/** Outputs the resulting syntax-highlighted markup to the DOM. */
52
55
#render( ) {
@@ -76,6 +79,27 @@ export default class HTMLCodeBlockElement extends HTMLElement {
76
79
this . #render( ) ;
77
80
}
78
81
82
+ /**
83
+ * The name of code block
84
+ * @returns - The value of the label attribute
85
+ */
86
+ get label ( ) {
87
+ return this . #label;
88
+ }
89
+
90
+ set label ( name : string ) {
91
+ // TODO: Accessiblity Treeにアクセシブルネームを提供する
92
+ this . #label = name || '' ;
93
+
94
+ if ( this . #label) {
95
+ this . setAttribute ( 'label' , name ) ;
96
+ } else {
97
+ this . removeAttribute ( 'label' ) ;
98
+ }
99
+
100
+ this . #render( ) ;
101
+ }
102
+
79
103
/**
80
104
* Language Mode
81
105
* @returns - The value of the language attribute
@@ -96,10 +120,32 @@ export default class HTMLCodeBlockElement extends HTMLElement {
96
120
this . #render( ) ;
97
121
}
98
122
123
+ /**
124
+ * Flag to display the UI
125
+ * @returns - With or without controls attribute
126
+ * */
127
+ get controls ( ) {
128
+ return this . #controls;
129
+ }
130
+
131
+ set controls ( flag : boolean ) {
132
+ // TODO: コピーボタン、ラベルの表示切り替え
133
+ this . #controls = flag ;
134
+
135
+ if ( this . #controls) {
136
+ this . setAttribute ( 'controls' , '' ) ;
137
+ } else {
138
+ this . removeAttribute ( 'controls' ) ;
139
+ }
140
+
141
+ this . #render( ) ;
142
+ }
143
+
99
144
static get observedAttributes ( ) {
100
145
return [
146
+ 'label' ,
101
147
'language' ,
102
- // 'controls',
148
+ 'controls' ,
103
149
] ;
104
150
}
105
151
@@ -115,10 +161,16 @@ export default class HTMLCodeBlockElement extends HTMLElement {
115
161
// When the value of the attribute being observed changes,
116
162
// pass value to accessors.
117
163
switch ( attrName ) {
164
+ // string
165
+ case 'label' :
118
166
case 'language' :
119
167
this [ attrName ] = newValue || '' ;
120
168
121
169
break ;
170
+
171
+ // boolean
172
+ case 'controls' :
173
+ this [ attrName ] = typeof newValue === 'string' ;
122
174
}
123
175
}
124
176
@@ -141,6 +193,8 @@ export default class HTMLCodeBlockElement extends HTMLElement {
141
193
return slot ;
142
194
}
143
195
const slots = [
196
+ mkslot ( 'label' ) ,
197
+ mkslot ( 'copy-button' ) ,
144
198
mkslot ( 'code' ) ,
145
199
] ;
146
200
const pre = document . createElement ( 'pre' ) ;
@@ -153,7 +207,9 @@ export default class HTMLCodeBlockElement extends HTMLElement {
153
207
154
208
// Hard private props initialize
155
209
this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) ;
210
+ this . #label = this . getAttribute ( 'label' ) || '' ;
156
211
this . #language = this . getAttribute ( 'language' ) || '' ;
212
+ this . #controls = this . getAttribute ( 'controls' ) !== null ;
157
213
this . #shadowRoot = this . attachShadow ( {
158
214
mode : 'closed' ,
159
215
} ) ;
0 commit comments