11import { Component , ComponentInterface , Element , Event , EventEmitter , Host , Prop , State , Watch , h } from '@stencil/core' ;
22
33import { getIonMode } from '../../global/ionic-global' ;
4+ import { Attributes , inheritAttributes } from '../../utils/helpers' ;
45
56/**
67 * @part image - The inner `img` element.
@@ -13,6 +14,7 @@ import { getIonMode } from '../../global/ionic-global';
1314export class Img implements ComponentInterface {
1415
1516 private io ?: IntersectionObserver ;
17+ private inheritedAttributes : Attributes = { } ;
1618
1719 @Element ( ) el ! : HTMLElement ;
1820
@@ -45,6 +47,10 @@ export class Img implements ComponentInterface {
4547 /** Emitted when the img fails to load */
4648 @Event ( ) ionError ! : EventEmitter < void > ;
4749
50+ componentWillLoad ( ) {
51+ this . inheritedAttributes = inheritAttributes ( this . el , [ 'draggable' ] ) ;
52+ }
53+
4854 componentDidLoad ( ) {
4955 this . addIO ( ) ;
5056 }
@@ -100,17 +106,38 @@ export class Img implements ComponentInterface {
100106 }
101107
102108 render ( ) {
109+ const { loadSrc, alt, onLoad, loadError, inheritedAttributes } = this ;
110+ const { draggable } = inheritedAttributes ;
103111 return (
104112 < Host class = { getIonMode ( this ) } >
105113 < img
106114 decoding = "async"
107- src = { this . loadSrc }
108- alt = { this . alt }
109- onLoad = { this . onLoad }
110- onError = { this . loadError }
115+ src = { loadSrc }
116+ alt = { alt }
117+ onLoad = { onLoad }
118+ onError = { loadError }
111119 part = "image"
120+ draggable = { isDraggable ( draggable ) }
112121 />
113122 </ Host >
114123 ) ;
115124 }
116125}
126+
127+ /**
128+ * Enumerated strings must be set as booleans
129+ * as Stencil will not render 'false' in the DOM.
130+ * The need to explicitly render draggable="true"
131+ * as only certain elements are draggable by default.
132+ * https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable.
133+ */
134+ const isDraggable = ( draggable ?: string ) : boolean | undefined => {
135+ switch ( draggable ) {
136+ case 'true' :
137+ return true ;
138+ case 'false' :
139+ return false ;
140+ default :
141+ return undefined ;
142+ }
143+ }
0 commit comments