1
1
import type { View } from '@ember/-internals/glimmer/lib/renderer' ;
2
- import { get , nativeDescDecorator , PROPERTY_DID_CHANGE } from '@ember/-internals/metal' ;
2
+ import {
3
+ descriptorForProperty ,
4
+ get ,
5
+ nativeDescDecorator ,
6
+ PROPERTY_DID_CHANGE ,
7
+ } from '@ember/-internals/metal' ;
3
8
import type { PropertyDidChange } from '@ember/-internals/metal/lib/property_events' ;
4
9
import { getOwner } from '@ember/-internals/owner' ;
5
10
import { TargetActionSupport } from '@ember/-internals/runtime' ;
6
11
import {
7
12
ActionSupport ,
8
13
addChildView ,
9
- ClassNamesSupport ,
10
14
CoreView ,
11
15
EventDispatcher ,
12
16
getChildViews ,
@@ -34,6 +38,8 @@ import {
34
38
// Keep track of which component classes have already been processed for lazy event setup.
35
39
let lazyEventsProcessed = new WeakMap < EventDispatcher , WeakSet < object > > ( ) ;
36
40
41
+ const EMPTY_ARRAY = Object . freeze ( [ ] ) ;
42
+
37
43
/**
38
44
@module @ember /component
39
45
*/
@@ -767,7 +773,6 @@ declare const SIGNATURE: unique symbol;
767
773
@class Component
768
774
@extends Ember.CoreView
769
775
@uses Ember.TargetActionSupport
770
- @uses Ember.ClassNamesSupport
771
776
@uses Ember.ActionSupport
772
777
@uses Ember.ViewMixin
773
778
@uses Ember.ViewStateSupport
@@ -778,7 +783,6 @@ declare const SIGNATURE: unique symbol;
778
783
interface Component < S = unknown >
779
784
extends CoreView ,
780
785
ViewStateSupport ,
781
- ClassNamesSupport ,
782
786
TargetActionSupport ,
783
787
ActionSupport ,
784
788
ViewMixin ,
@@ -787,7 +791,6 @@ interface Component<S = unknown>
787
791
class Component < S = unknown >
788
792
extends CoreView . extend (
789
793
ViewStateSupport ,
790
- ClassNamesSupport ,
791
794
TargetActionSupport ,
792
795
ActionSupport ,
793
796
ViewMixin ,
@@ -800,7 +803,12 @@ class Component<S = unknown>
800
803
didUpdateAttrs ( ) { } ,
801
804
willRender ( ) { } ,
802
805
willUpdate ( ) { } ,
803
- } as ComponentMethods
806
+ } as ComponentMethods ,
807
+ {
808
+ concatenatedProperties : [ 'classNames' , 'classNameBindings' ] ,
809
+ classNames : EMPTY_ARRAY ,
810
+ classNameBindings : EMPTY_ARRAY ,
811
+ }
804
812
)
805
813
implements PropertyDidChange
806
814
{
@@ -816,6 +824,77 @@ class Component<S = unknown>
816
824
declare [ IS_DISPATCHING_ATTRS ] : boolean ;
817
825
declare [ DIRTY_TAG ] : DirtyableTag ;
818
826
827
+ /**
828
+ Standard CSS class names to apply to the view's outer element. This
829
+ property automatically inherits any class names defined by the view's
830
+ superclasses as well.
831
+
832
+ @property classNames
833
+ @type Array
834
+ @default ['ember-view']
835
+ @public
836
+ */
837
+ declare classNames : string [ ] ;
838
+
839
+ /**
840
+ A list of properties of the view to apply as class names. If the property
841
+ is a string value, the value of that string will be applied as a class
842
+ name.
843
+
844
+ ```javascript
845
+ // Applies the 'high' class to the view element
846
+ import Component from '@ember/component';
847
+ Component.extend({
848
+ classNameBindings: ['priority'],
849
+ priority: 'high'
850
+ });
851
+ ```
852
+
853
+ If the value of the property is a Boolean, the name of that property is
854
+ added as a dasherized class name.
855
+
856
+ ```javascript
857
+ // Applies the 'is-urgent' class to the view element
858
+ import Component from '@ember/component';
859
+ Component.extend({
860
+ classNameBindings: ['isUrgent'],
861
+ isUrgent: true
862
+ });
863
+ ```
864
+
865
+ If you would prefer to use a custom value instead of the dasherized
866
+ property name, you can pass a binding like this:
867
+
868
+ ```javascript
869
+ // Applies the 'urgent' class to the view element
870
+ import Component from '@ember/component';
871
+ Component.extend({
872
+ classNameBindings: ['isUrgent:urgent'],
873
+ isUrgent: true
874
+ });
875
+ ```
876
+
877
+ If you would like to specify a class that should only be added when the
878
+ property is false, you can declare a binding like this:
879
+
880
+ ```javascript
881
+ // Applies the 'disabled' class to the view element
882
+ import Component from '@ember/component';
883
+ Component.extend({
884
+ classNameBindings: ['isEnabled::disabled'],
885
+ isEnabled: false
886
+ });
887
+ ```
888
+
889
+ This list of properties is inherited from the component's superclasses as well.
890
+
891
+ @property classNameBindings
892
+ @type Array
893
+ @default []
894
+ @public
895
+ */
896
+ declare classNameBindings : string [ ] ;
897
+
819
898
init ( properties ?: object | undefined ) {
820
899
super . init ( properties ) ;
821
900
@@ -869,6 +948,16 @@ class Component<S = unknown>
869
948
! eventNames . length
870
949
) ;
871
950
}
951
+
952
+ assert (
953
+ `Only arrays are allowed for 'classNameBindings'` ,
954
+ descriptorForProperty ( this , 'classNameBindings' ) === undefined &&
955
+ Array . isArray ( this . classNameBindings )
956
+ ) ;
957
+ assert (
958
+ `Only arrays of static class strings are allowed for 'classNames'. For dynamic classes, use 'classNameBindings'.` ,
959
+ descriptorForProperty ( this , 'classNames' ) === undefined && Array . isArray ( this . classNames )
960
+ ) ;
872
961
}
873
962
874
963
__dispatcher ?: EventDispatcher | null ;
0 commit comments