icon.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { Directive, ElementRef, HostBinding, Input, Renderer } from '@angular/core';
  2. import { isTrueProperty } from '../../util/util';
  3. import { Config } from '../../config/config';
  4. import { Ion } from '../ion';
  5. /**
  6. * @name Icon
  7. * @description
  8. * Icons can be used on their own, or inside of a number of Ionic components.
  9. * For a full list of available icons, check out the
  10. * [Ionicons docs](../../../../ionicons).
  11. *
  12. * One feature of Ionicons in Ionic is when icon names are set, the actual icon
  13. * which is rendered can change slightly depending on the mode the app is
  14. * running from. For example, by setting the icon name of `alarm`, on iOS the
  15. * icon will automatically apply `ios-alarm`, and on Material Design it will
  16. * automatically apply `md-alarm`. This allows the developer to write the
  17. * markup once while Ionic applies the appropriate icon based on the mode.
  18. *
  19. * @usage
  20. * ```html
  21. * <!-- automatically uses the correct "star" icon depending on the mode -->
  22. * <ion-icon name="star"></ion-icon>
  23. *
  24. * <!-- explicity set the icon for each mode -->
  25. * <ion-icon ios="ios-home" md="md-home"></ion-icon>
  26. *
  27. * <!-- always use the same icon, no matter what the mode -->
  28. * <ion-icon name="ios-clock"></ion-icon>
  29. * <ion-icon name="logo-twitter"></ion-icon>
  30. * ```
  31. *
  32. * @demo /docs/demos/src/icon/
  33. * @see {@link /docs/components#icons Icon Component Docs}
  34. *
  35. */
  36. export class Icon extends Ion {
  37. constructor(config, elementRef, renderer) {
  38. super(config, elementRef, renderer, 'icon');
  39. /** @hidden */
  40. this._isActive = true;
  41. /** @hidden */
  42. this._name = '';
  43. /** @hidden */
  44. this._ios = '';
  45. /** @hidden */
  46. this._md = '';
  47. /** @hidden */
  48. this._css = '';
  49. /**
  50. * @hidden
  51. */
  52. this._hidden = false;
  53. this._iconMode = config.get('iconMode');
  54. }
  55. /**
  56. * @hidden
  57. */
  58. ngOnDestroy() {
  59. if (this._css) {
  60. this.setElementClass(this._css, false);
  61. }
  62. }
  63. /**
  64. * @input {string} Specifies which icon to use. The appropriate icon will be used based on the mode.
  65. * For more information, see [Ionicons](/docs/ionicons/).
  66. */
  67. get name() {
  68. return this._name;
  69. }
  70. set name(val) {
  71. if (!(/^md-|^ios-|^logo-/.test(val))) {
  72. // this does not have one of the defaults
  73. // so lets auto add in the mode prefix for them
  74. this._name = this._iconMode + '-' + val;
  75. }
  76. else {
  77. this._name = val;
  78. }
  79. this.update();
  80. }
  81. /**
  82. * @input {string} Specifies which icon to use on `ios` mode.
  83. */
  84. get ios() {
  85. return this._ios;
  86. }
  87. set ios(val) {
  88. this._ios = val;
  89. this.update();
  90. }
  91. /**
  92. * @input {string} Specifies which icon to use on `md` mode.
  93. */
  94. get md() {
  95. return this._md;
  96. }
  97. set md(val) {
  98. this._md = val;
  99. this.update();
  100. }
  101. /**
  102. * @input {boolean} If true, the icon is styled with an "active" appearance.
  103. * An active icon is filled in, and an inactive icon is the outline of the icon.
  104. * The `isActive` property is largely used by the tabbar. Only affects `ios` icons.
  105. */
  106. get isActive() {
  107. return this._isActive;
  108. }
  109. set isActive(val) {
  110. this._isActive = isTrueProperty(val);
  111. this.update();
  112. }
  113. /**
  114. * @hidden
  115. */
  116. update() {
  117. let iconName;
  118. if (this._ios && this._iconMode === 'ios') {
  119. iconName = this._ios;
  120. }
  121. else if (this._md && this._iconMode === 'md') {
  122. iconName = this._md;
  123. }
  124. else {
  125. iconName = this._name;
  126. }
  127. let hidden = this._hidden = (iconName === null);
  128. if (hidden) {
  129. return;
  130. }
  131. let iconMode = iconName.split('-', 2)[0];
  132. if (iconMode === 'ios' &&
  133. !this._isActive &&
  134. iconName.indexOf('logo-') < 0 &&
  135. iconName.indexOf('-outline') < 0) {
  136. iconName += '-outline';
  137. }
  138. let css = 'ion-' + iconName;
  139. if (this._css === css) {
  140. return;
  141. }
  142. if (this._css) {
  143. this.setElementClass(this._css, false);
  144. }
  145. this._css = css;
  146. this.setElementClass(css, true);
  147. let label = iconName
  148. .replace('ios-', '')
  149. .replace('md-', '')
  150. .replace('-', ' ');
  151. this.setElementAttribute('aria-label', label);
  152. }
  153. }
  154. Icon.decorators = [
  155. { type: Directive, args: [{
  156. selector: 'ion-icon',
  157. host: {
  158. 'role': 'img'
  159. }
  160. },] },
  161. ];
  162. /** @nocollapse */
  163. Icon.ctorParameters = () => [
  164. { type: Config, },
  165. { type: ElementRef, },
  166. { type: Renderer, },
  167. ];
  168. Icon.propDecorators = {
  169. 'name': [{ type: Input },],
  170. 'ios': [{ type: Input },],
  171. 'md': [{ type: Input },],
  172. 'isActive': [{ type: Input },],
  173. '_hidden': [{ type: HostBinding, args: ['class.hide',] },],
  174. };
  175. //# sourceMappingURL=icon.js.map