UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

1 lines 4.56 kB
{"version":3,"file":"_directionality-chunk.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/cdk/bidi/dir-document-token.ts","../../../../../darwin_arm64-fastbuild-ST-fdfa778d11ba/bin/src/cdk/bidi/directionality.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {inject, InjectionToken, DOCUMENT} from '@angular/core';\n\n/**\n * Injection token used to inject the document into Directionality.\n * This is used so that the value can be faked in tests.\n *\n * We can't use the real document in tests because changing the real `dir` causes geometry-based\n * tests in Safari to fail.\n *\n * We also can't re-provide the DOCUMENT token from platform-browser because the unit tests\n * themselves use things like `querySelector` in test code.\n *\n * This token is defined in a separate file from Directionality as a workaround for\n * https://github.com/angular/angular/issues/22559\n *\n * @docs-private\n */\nexport const DIR_DOCUMENT = new InjectionToken<Document>('cdk-dir-doc', {\n providedIn: 'root',\n factory: () => inject(DOCUMENT),\n});\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {EventEmitter, Service, OnDestroy, inject, signal} from '@angular/core';\nimport {DIR_DOCUMENT} from './dir-document-token';\n\nexport type Direction = 'ltr' | 'rtl';\n\n/** Regex that matches locales with an RTL script. Taken from `goog.i18n.bidi.isRtlLanguage`. */\nconst RTL_LOCALE_PATTERN =\n /^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Adlm|Arab|Hebr|Nkoo|Rohg|Thaa))(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)/i;\n\n/** Resolves a string value to a specific direction. */\nexport function _resolveDirectionality(rawValue: string): Direction {\n const value = rawValue?.toLowerCase() || '';\n\n if (value === 'auto' && typeof navigator !== 'undefined' && navigator?.language) {\n return RTL_LOCALE_PATTERN.test(navigator.language) ? 'rtl' : 'ltr';\n }\n\n return value === 'rtl' ? 'rtl' : 'ltr';\n}\n\n/**\n * The directionality (LTR / RTL) context for the application (or a subtree of it).\n * Exposes the current direction and a stream of direction changes.\n */\n@Service()\nexport class Directionality implements OnDestroy {\n /** The current 'ltr' or 'rtl' value. */\n get value() {\n return this.valueSignal();\n }\n\n /**\n * The current 'ltr' or 'rtl' value.\n */\n readonly valueSignal = signal<Direction>('ltr');\n\n /** Stream that emits whenever the 'ltr' / 'rtl' state changes. */\n readonly change = new EventEmitter<Direction>();\n\n constructor() {\n const _document = inject(DIR_DOCUMENT, {optional: true});\n\n if (_document) {\n const bodyDir = _document.body ? _document.body.dir : null;\n const htmlDir = _document.documentElement ? _document.documentElement.dir : null;\n this.valueSignal.set(_resolveDirectionality(bodyDir || htmlDir || 'ltr'));\n }\n }\n\n ngOnDestroy() {\n this.change.complete();\n }\n}\n"],"names":[],"mappings":";;;MAyBa,YAAY,GAAG,IAAI,cAAc,CAAW,aAAa,EAAE;AACtE,EAAA,UAAU,EAAE,MAAM;AAClB,EAAA,OAAO,EAAE,MAAM,MAAM,CAAC,QAAQ;AAC/B,CAAA;;ACdD,MAAM,kBAAkB,GACtB,oHAAoH;AAGhH,SAAU,sBAAsB,CAAC,QAAgB,EAAA;EACrD,MAAM,KAAK,GAAG,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE;AAE3C,EAAA,IAAI,KAAK,KAAK,MAAM,IAAI,OAAO,SAAS,KAAK,WAAW,IAAI,SAAS,EAAE,QAAQ,EAAE;IAC/E,OAAO,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,KAAK,GAAG,KAAK;AACpE,EAAA;AAEA,EAAA,OAAO,KAAK,KAAK,KAAK,GAAG,KAAK,GAAG,KAAK;AACxC;MAOa,cAAc,CAAA;AAEzB,EAAA,IAAI,KAAK,GAAA;AACP,IAAA,OAAO,IAAI,CAA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