@salesforcedevs/dx-components 1.4.0 → 1.5.0-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dx-components",
3
- "version": "1.4.0",
3
+ "version": "1.5.0-alpha",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -45,6 +45,5 @@
45
45
  },
46
46
  "volta": {
47
47
  "node": "18.18.0"
48
- },
49
- "gitHead": "052e1a61657dec2c9299570814e2d7c561517fe2"
48
+ }
50
49
  }
@@ -1,4 +1,5 @@
1
1
  /* eslint-disable @lwc/lwc/no-document-query */
2
+ import { OPTIMIZELY_INSTANCE, USER_ID } from "dx/featureFlag";
2
3
  import { LightningElement } from "lwc";
3
4
 
4
5
  export const LOCAL_STORAGE_KEY = "dx-dark-mode-theme";
@@ -29,10 +30,7 @@ declare module globalThis {
29
30
 
30
31
  export const DARK_MODE_THEMES = ["dark", "light"];
31
32
  export const DARKMODE_TOGGLE_EVENT_NAME = "UPDATE_DARK_MODE";
32
-
33
- export const isDarkModeEnabled = () => {
34
- return process.env.DARK_MODE;
35
- };
33
+ const DARK_MODE_FEATURE_FLAG = "dark-mode";
36
34
 
37
35
  export const dispatchDarkModeToggleEvent = () => {
38
36
  window.dispatchEvent(new Event(DARKMODE_TOGGLE_EVENT_NAME));
@@ -55,32 +53,40 @@ export const setLocalStorageDarkModeSetting = (theme: string) => {
55
53
 
56
54
  export default class DarkModeManager extends LightningElement {
57
55
  renderedCallback(): void {
58
- if (
59
- isDarkModeEnabled() &&
60
- !globalThis.singletonDarkModeManagerRendered
61
- ) {
62
- const theme = getLocalStorageDarkModeSetting();
63
- if (theme) {
64
- if (DARK_MODE_THEMES.includes(theme)) {
65
- setTheme(theme);
56
+ OPTIMIZELY_INSTANCE!.onReady().then(({ success }) => {
57
+ if (
58
+ success &&
59
+ OPTIMIZELY_INSTANCE!.isFeatureEnabled(
60
+ DARK_MODE_FEATURE_FLAG,
61
+ USER_ID
62
+ )
63
+ ) {
64
+ if (!globalThis.singletonDarkModeManagerRendered) {
65
+ const theme = getLocalStorageDarkModeSetting();
66
+ if (theme) {
67
+ if (DARK_MODE_THEMES.includes(theme)) {
68
+ setTheme(theme);
69
+ }
70
+ } else if (isBrowserDarkModeEnabled()) {
71
+ setTheme("dark");
72
+ }
73
+ window.addEventListener(
74
+ DARKMODE_TOGGLE_EVENT_NAME,
75
+ onDarkModeToggle
76
+ );
77
+ globalThis.singletonDarkModeManagerRendered = true;
66
78
  }
67
- } else if (isBrowserDarkModeEnabled()) {
68
- setTheme("dark");
69
79
  }
70
- window.addEventListener(
71
- DARKMODE_TOGGLE_EVENT_NAME,
72
- onDarkModeToggle
73
- );
74
- globalThis.singletonDarkModeManagerRendered = true;
75
- }
80
+ });
76
81
  }
77
82
 
78
83
  disconnectedCallback(): void {
79
- if (isDarkModeEnabled()) {
84
+ if (globalThis.singletonDarkModeManagerRendered) {
80
85
  window.removeEventListener(
81
86
  DARKMODE_TOGGLE_EVENT_NAME,
82
87
  onDarkModeToggle
83
88
  );
89
+ globalThis.singletonDarkModeManagerRendered = false;
84
90
  }
85
91
  }
86
92
  }
@@ -3,37 +3,42 @@ import optimizelySdk from "@optimizely/optimizely-sdk";
3
3
 
4
4
  // This is needed to specify user context (such as logged in, admin, ...etc).
5
5
  // leave this as hard-coded for now until we create audiences in Optimizely.
6
- const userId = "user123";
6
+ export const USER_ID = "sf-dwo";
7
+ const LOCAL_ENVIRONMENT_SDK_KEY = "DzwQxncbVQsbMnXfjpasL";
8
+ const SDK_KEY = process.env.OPTIMIZELY_SDK_KEY;
9
+ const DATAFILE_ACCESS_TOKEN = process.env.OPTIMIZELY_DATAFILE_ACCESS_TOKEN;
10
+ const DATAFILE_OPTIONS =
11
+ SDK_KEY && DATAFILE_ACCESS_TOKEN
12
+ ? {
13
+ urlTemplate: `https://config.optimizely.com/datafiles/auth/${SDK_KEY}.json`,
14
+ datafileAccessToken: DATAFILE_ACCESS_TOKEN
15
+ }
16
+ : undefined;
17
+ export const OPTIMIZELY_INSTANCE = optimizelySdk.createInstance({
18
+ sdkKey: SDK_KEY || LOCAL_ENVIRONMENT_SDK_KEY,
19
+ datafileOptions: DATAFILE_OPTIONS
20
+ });
7
21
 
8
22
  export default class FeatureFlag extends LightningElement {
9
23
  @api flag!: string;
10
24
 
11
- private optimizelyInstance: any;
12
-
13
25
  private showElements(slot: any) {
14
26
  slot.classList.remove("hidden");
15
27
  }
16
28
 
17
- connectedCallback(): void {
18
- this.optimizelyInstance = optimizelySdk.createInstance({
19
- sdkKey: process.env.OPTIMIZELY_SDK_KEY
20
- });
21
- }
22
-
29
+ /*
30
+ Note that this component only stops the element from being redered to the screen.
31
+ If you want to stop the execution of the code for the element, you will need to also check
32
+ the feature flag in that component.
33
+ */
23
34
  renderedCallback(): void {
24
35
  const slotEl = this.template.querySelector("slot") as any;
25
36
 
26
- this.optimizelyInstance?.onReady().then(({ success, reason }: any) => {
27
- if (!success) {
28
- console.log(
29
- `Optimizely client initialization unsuccessful, reason: ${reason}`
30
- );
31
- return;
32
- }
33
- const user = this.optimizelyInstance?.createUserContext(userId);
34
- const decision = user?.decide(this.flag);
35
-
36
- if (decision?.enabled) {
37
+ OPTIMIZELY_INSTANCE!.onReady().then(({ success }) => {
38
+ if (
39
+ success &&
40
+ OPTIMIZELY_INSTANCE!.isFeatureEnabled(this.flag, USER_ID)
41
+ ) {
37
42
  this.showElements(slotEl);
38
43
  }
39
44
  });
@@ -21,14 +21,14 @@
21
21
  <div>
22
22
  <slot name="language-selector"></slot>
23
23
  </div>
24
- <template lwc:if={isDarkModeEnabled}>
24
+ <dx-feature-flag flag="dark_mode">
25
25
  <div
26
26
  class="dark-mode-button"
27
27
  onclick={onDarkModeButtonClick}
28
28
  >
29
29
  <div class="dark-mode-icon"></div>
30
30
  </div>
31
- </template>
31
+ </dx-feature-flag>
32
32
  </div>
33
33
  </div>
34
34
  <div class="container-layout">
@@ -5,10 +5,7 @@ import { toJson } from "dxUtils/normalizers";
5
5
  import { track } from "dxUtils/analytics";
6
6
  import svgs from "./svgs";
7
7
  import ImageAndLabel from "dx/imageAndLabel";
8
- import {
9
- dispatchDarkModeToggleEvent,
10
- isDarkModeEnabled
11
- } from "dx/darkModeManager";
8
+ import { dispatchDarkModeToggleEvent } from "dx/darkModeManager";
12
9
 
13
10
  export default class FeaturedContentHeader extends LightningElement {
14
11
  @api label?: string | null = null;
@@ -95,7 +92,7 @@ export default class FeaturedContentHeader extends LightningElement {
95
92
  }
96
93
 
97
94
  private get isDarkModeEnabled() {
98
- return this.enableDarkModeToggle && isDarkModeEnabled();
95
+ return this.enableDarkModeToggle;
99
96
  }
100
97
 
101
98
  private get hasPlainImage() {
package/LICENSE DELETED
@@ -1,12 +0,0 @@
1
- Copyright (c) 2020, Salesforce.com, Inc.
2
- All rights reserved.
3
-
4
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
-
6
- * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
-
8
- * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
-
10
- * Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
-
12
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.