@codecademy/brand 2.5.0-alpha.975930aaa2.0 → 3.0.0-alpha.2ba7921cce.0

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.
Files changed (72) hide show
  1. package/dist/AppHeader/AppHeaderElements/AppHeaderCatalogSection/elements.d.ts +0 -5
  2. package/dist/AppHeader/AppHeaderElements/AppHeaderCatalogSection/elements.js +12 -56
  3. package/dist/AppHeader/AppHeaderElements/AppHeaderCatalogSection/index.js +3 -5
  4. package/dist/AppHeader/AppHeaderElements/AppHeaderLinkSections/index.js +0 -0
  5. package/dist/CareerPathCard/index.d.ts +1 -4
  6. package/dist/CareerPathCard/index.js +7 -15
  7. package/dist/CertificationPathCard/index.d.ts +1 -4
  8. package/dist/CertificationPathCard/index.js +9 -17
  9. package/dist/ContentGroupBaseCard/index.d.ts +3 -5
  10. package/dist/ContentGroupBaseCard/index.js +72 -129
  11. package/dist/ContentGroupBaseCard/shared.js +23 -29
  12. package/dist/CourseCard/index.d.ts +1 -4
  13. package/dist/CourseCard/index.js +11 -15
  14. package/dist/GlobalHeader/GlobalHeaderItems.js +1 -1
  15. package/dist/GlobalHeader/index.d.ts +0 -1
  16. package/dist/GlobalHeader/index.js +37 -41
  17. package/dist/GlobalHeader/types.d.ts +0 -1
  18. package/dist/LearningOutcomeAssessmentScores/AssessmentDetail.d.ts +8 -0
  19. package/dist/LearningOutcomeAssessmentScores/AssessmentDetail.js +55 -0
  20. package/dist/LearningOutcomeAssessmentScores/index.d.ts +8 -0
  21. package/dist/LearningOutcomeAssessmentScores/index.js +24 -0
  22. package/dist/LearningOutcomeCard/index.d.ts +10 -12
  23. package/dist/LearningOutcomeCard/index.js +168 -143
  24. package/dist/LearningOutcomeFlyout/LearningOutcomeCardList.d.ts +6 -0
  25. package/dist/LearningOutcomeFlyout/LearningOutcomeCardList.js +23 -0
  26. package/dist/LearningOutcomeFlyout/index.d.ts +38 -0
  27. package/dist/LearningOutcomeFlyout/index.js +183 -0
  28. package/dist/LearningOutcomeTile/index.d.ts +2 -1
  29. package/dist/LearningOutcomeTile/index.js +2 -4
  30. package/dist/LearningOutcomeTile/types.d.ts +0 -1
  31. package/dist/SkillPathCard/index.d.ts +1 -4
  32. package/dist/SkillPathCard/index.js +7 -15
  33. package/dist/index.d.ts +2 -1
  34. package/dist/index.js +2 -1
  35. package/dist/stories/Molecules/ContentGroupBaseCard.stories.js +1 -3
  36. package/dist/stories/Molecules/CurriculumCards/CareerPathCard.stories.js +1 -3
  37. package/dist/stories/Molecules/CurriculumCards/CertificationPathCard.stories.js +1 -3
  38. package/dist/stories/Molecules/CurriculumCards/CourseCard.stories.js +1 -3
  39. package/dist/stories/Molecules/CurriculumCards/SkillPathCard.stories.js +1 -3
  40. package/dist/stories/Molecules/LearningOutcomeAssessmentScores.stories.d.ts +6 -0
  41. package/dist/stories/Molecules/LearningOutcomeAssessmentScores.stories.js +23 -0
  42. package/dist/stories/Molecules/LearningOutcomeCard.stories.d.ts +7 -2
  43. package/dist/stories/Molecules/LearningOutcomeCard.stories.js +168 -54
  44. package/dist/stories/Molecules/LearningOutcomeFlyout.stories.d.ts +5 -0
  45. package/dist/stories/Molecules/LearningOutcomeFlyout.stories.js +44 -0
  46. package/package.json +1 -1
  47. package/dist/Recurly/context/NoProvider.d.ts +0 -8
  48. package/dist/Recurly/context/NoProvider.js +0 -7
  49. package/dist/Recurly/context/elements/index.d.ts +0 -26
  50. package/dist/Recurly/context/elements/index.js +0 -46
  51. package/dist/Recurly/context/elements/index.test.js +0 -88
  52. package/dist/Recurly/context/index.d.ts +0 -2
  53. package/dist/Recurly/context/index.js +0 -2
  54. package/dist/Recurly/context/recurly/index.d.ts +0 -30
  55. package/dist/Recurly/context/recurly/index.js +0 -50
  56. package/dist/Recurly/context/recurly/index.test.js +0 -63
  57. package/dist/Recurly/context/recurly/loadRecurly.d.ts +0 -6
  58. package/dist/Recurly/context/recurly/loadRecurly.js +0 -22
  59. package/dist/Recurly/context/recurly/loadRecurly.test.js +0 -57
  60. package/dist/Recurly/context/recurly/w.d.ts +0 -14
  61. package/dist/Recurly/context/recurly/w.js +0 -4
  62. package/dist/Recurly/fields/RecurlyField.d.ts +0 -9
  63. package/dist/Recurly/fields/RecurlyField.js +0 -57
  64. package/dist/Recurly/fields/elements.d.ts +0 -17
  65. package/dist/Recurly/fields/elements.js +0 -106
  66. package/dist/Recurly/fields/elements.test.js +0 -79
  67. package/dist/Recurly/fields/index.d.ts +0 -2
  68. package/dist/Recurly/fields/index.js +0 -2
  69. package/dist/Recurly/index.d.ts +0 -2
  70. package/dist/Recurly/index.js +0 -2
  71. package/dist/stories/Molecules/Recurly.stories.d.ts +0 -5
  72. package/dist/stories/Molecules/Recurly.stories.js +0 -56
@@ -1,75 +1,189 @@
1
- import { Background } from '@codecademy/gamut-styles';
1
+ import { FlexBox } from '@codecademy/gamut';
2
2
  import { LearningOutcomeCard } from '../../LearningOutcomeCard';
3
- import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
4
4
  const meta = {
5
5
  component: LearningOutcomeCard,
6
6
  args: {
7
+ outcome: 'Implement sorting algorithms in Java.'
8
+ }
9
+ };
10
+ export default meta;
11
+ export const Base = {
12
+ render: args => /*#__PURE__*/_jsxs(FlexBox, {
13
+ gap: 16,
14
+ children: [/*#__PURE__*/_jsx(LearningOutcomeCard, {
15
+ ...args,
16
+ level: 1
17
+ }), /*#__PURE__*/_jsx(LearningOutcomeCard, {
18
+ ...args,
19
+ level: 2
20
+ }), /*#__PURE__*/_jsx(LearningOutcomeCard, {
21
+ ...args,
22
+ level: 3
23
+ })]
24
+ }),
25
+ name: 'Base card'
26
+ };
27
+ export const BaseCardWithHover = {
28
+ render: args => /*#__PURE__*/_jsxs(FlexBox, {
29
+ gap: 16,
30
+ children: [/*#__PURE__*/_jsx(LearningOutcomeCard, {
31
+ ...args,
32
+ level: 1,
33
+ isInteractive: true
34
+ }), /*#__PURE__*/_jsx(LearningOutcomeCard, {
35
+ ...args,
36
+ level: 2,
37
+ isInteractive: true
38
+ }), /*#__PURE__*/_jsx(LearningOutcomeCard, {
39
+ ...args,
40
+ level: 3,
41
+ isInteractive: true
42
+ })]
43
+ }),
44
+ name: 'Base card with hover'
45
+ };
46
+ export const BaseCardWithTopics = {
47
+ render: args => /*#__PURE__*/_jsxs(FlexBox, {
48
+ gap: 16,
49
+ children: [/*#__PURE__*/_jsx(LearningOutcomeCard, {
50
+ ...args,
51
+ level: 1,
52
+ skills: [{
53
+ title: 'java'
54
+ }, {
55
+ title: 'skill'
56
+ }, {
57
+ title: 'computer science'
58
+ }]
59
+ }), /*#__PURE__*/_jsx(LearningOutcomeCard, {
60
+ ...args,
61
+ level: 2,
62
+ skills: [{
63
+ title: 'java'
64
+ }, {
65
+ title: 'skill'
66
+ }, {
67
+ title: 'computer science'
68
+ }]
69
+ }), /*#__PURE__*/_jsx(LearningOutcomeCard, {
70
+ ...args,
71
+ level: 3,
72
+ skills: [{
73
+ title: 'java'
74
+ }, {
75
+ title: 'skill'
76
+ }, {
77
+ title: 'computer science'
78
+ }]
79
+ })]
80
+ }),
81
+ name: 'Base card with topics'
82
+ };
83
+ export const EmphasizedCard = {
84
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
85
+ ...args,
7
86
  level: 1,
8
- outcome: 'Implement sorting algorithms in Java.',
87
+ mode: "emphasized"
88
+ }),
89
+ name: 'Emphasized card'
90
+ };
91
+ export const EmphasizedCardWithHover = {
92
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
93
+ ...args,
94
+ level: 2,
95
+ mode: "emphasized",
96
+ isInteractive: true
97
+ }),
98
+ name: 'Emphasized card with hover'
99
+ };
100
+ export const EmphasizedCardWithTopics = {
101
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
102
+ ...args,
103
+ level: 3,
9
104
  skills: [{
10
105
  title: 'java'
11
106
  }, {
12
107
  title: 'skill'
13
108
  }, {
14
109
  title: 'computer science'
15
- }]
16
- }
17
- };
18
- export default meta;
19
- export const Default = {
20
- render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
21
- ...args
110
+ }],
111
+ mode: "emphasized",
112
+ isInteractive: true
22
113
  }),
23
- name: 'Learning Outcome Simple Card'
24
- };
25
- export const EmphasizedCard = {
26
- render: args => /*#__PURE__*/_jsx(Background, {
27
- bg: "navy",
28
- p: 24,
29
- children: /*#__PURE__*/_jsx(LearningOutcomeCard, {
30
- emphasized: true,
31
- ...args
32
- })
33
- }),
34
- name: 'Emphasized Card'
114
+ name: 'Emphasized card with topics'
35
115
  };
36
116
  export const EmphasizedCardWithProgress = {
37
- render: args => /*#__PURE__*/_jsx(Background, {
38
- bg: "navy",
39
- p: 24,
40
- children: /*#__PURE__*/_jsx(LearningOutcomeCard, {
41
- emphasized: true,
42
- progress: 60,
43
- ...args
44
- })
117
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
118
+ ...args,
119
+ level: 2,
120
+ progress: 60,
121
+ skills: [{
122
+ title: 'java'
123
+ }, {
124
+ title: 'skill'
125
+ }, {
126
+ title: 'computer science'
127
+ }],
128
+ mode: "emphasized",
129
+ isInteractive: true
45
130
  }),
46
- name: 'Emphasized card displaying progress, for use in the Learning Environment'
131
+ name: 'Emphasized card with progress'
47
132
  };
48
133
  export const EmphasizedCardWithProgress100 = {
49
- render: args => /*#__PURE__*/_jsx(Background, {
50
- bg: "navy",
51
- p: 24,
52
- children: /*#__PURE__*/_jsx(LearningOutcomeCard, {
53
- emphasized: true,
54
- progress: 100,
55
- ...args
56
- })
134
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
135
+ ...args,
136
+ level: 3,
137
+ progress: 100,
138
+ skills: [{
139
+ title: 'java'
140
+ }, {
141
+ title: 'skill'
142
+ }, {
143
+ title: 'computer science'
144
+ }],
145
+ mode: "emphasized",
146
+ isInteractive: true
57
147
  }),
58
- name: 'Emphasized card displaying 100% progress, for use in the Learning Environment'
148
+ name: 'Emphasized card with 100% progress'
59
149
  };
60
- export const EmphasizedCardWithAnimation = {
61
- render: args => /*#__PURE__*/_jsx(Background, {
62
- bg: "navy",
63
- p: 24,
64
- children: /*#__PURE__*/_jsx(LearningOutcomeCard, {
65
- emphasized: true,
66
- progress: 80,
67
- progressUpdateData: {
68
- previousProgress: 50,
69
- progressDuration: 500
70
- },
71
- ...args
72
- })
150
+ export const EmphasizedCardWithProgressAnimation = {
151
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
152
+ ...args,
153
+ level: 2,
154
+ progress: 80,
155
+ progressUpdateData: {
156
+ previousProgress: 50,
157
+ progressDuration: 500
158
+ },
159
+ skills: [{
160
+ title: 'java'
161
+ }, {
162
+ title: 'skill'
163
+ }, {
164
+ title: 'computer science'
165
+ }],
166
+ mode: "emphasized"
167
+ }),
168
+ name: 'Emphasized card with progress with animation'
169
+ };
170
+ export const EmphasizedCardWithProgress100Animation = {
171
+ render: args => /*#__PURE__*/_jsx(LearningOutcomeCard, {
172
+ ...args,
173
+ level: 2,
174
+ progress: 100,
175
+ progressUpdateData: {
176
+ previousProgress: 50,
177
+ completionDelay: 500
178
+ },
179
+ skills: [{
180
+ title: 'java'
181
+ }, {
182
+ title: 'skill'
183
+ }, {
184
+ title: 'computer science'
185
+ }],
186
+ mode: "emphasized"
73
187
  }),
74
- name: 'Emphasized card first displays 50% progress then animates until showing 80%, for use in the Learning Environment'
188
+ name: 'Emphasized card with 100% progress with animation'
75
189
  };
@@ -0,0 +1,5 @@
1
+ import type { Meta } from '@storybook/react';
2
+ import { LearningOutcomeFlyout } from '../../LearningOutcomeFlyout';
3
+ declare const meta: Meta<typeof LearningOutcomeFlyout>;
4
+ export default meta;
5
+ export declare const Example: () => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,44 @@
1
+ import { FillButton, FlexBox } from '@codecademy/gamut';
2
+ import { useState } from 'react';
3
+ import { LearningOutcomeFlyout } from '../../LearningOutcomeFlyout';
4
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
5
+ const meta = {
6
+ component: LearningOutcomeFlyout,
7
+ args: {}
8
+ };
9
+ export default meta;
10
+ export const Example = () => {
11
+ const [isExpanded, setIsExpanded] = useState(false);
12
+ const toggleOpen = () => setIsExpanded(!isExpanded);
13
+ return /*#__PURE__*/_jsxs(_Fragment, {
14
+ children: [/*#__PURE__*/_jsx(FillButton, {
15
+ onClick: toggleOpen,
16
+ children: "Open Flyout"
17
+ }), /*#__PURE__*/_jsx(FlexBox, {
18
+ width: "100%",
19
+ children: /*#__PURE__*/_jsx(LearningOutcomeFlyout, {
20
+ expanded: isExpanded,
21
+ learningOutcome: {
22
+ highestScore: 100,
23
+ highestScoreAchievedAt: '10/01',
24
+ latestScore: 68,
25
+ latestScoreAchievedAt: '10/01',
26
+ level: 2,
27
+ outcome: 'Understand the basics of React',
28
+ percentComplete: 75,
29
+ parentLOs: [{
30
+ id: '1',
31
+ outcome: 'Build a React component',
32
+ level: 3
33
+ }, {
34
+ id: '2',
35
+ outcome: 'Manage state in React',
36
+ level: 3
37
+ }]
38
+ },
39
+ learningOutcomeHref: "/learning-outcomes/123",
40
+ onClose: () => setIsExpanded(false)
41
+ })
42
+ })]
43
+ });
44
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/brand",
3
3
  "description": "Brand component library for Codecademy",
4
- "version": "2.5.0-alpha.975930aaa2.0",
4
+ "version": "3.0.0-alpha.2ba7921cce.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "dependencies": {
7
7
  "@emotion/is-prop-valid": "^1.2.1",
@@ -1,8 +0,0 @@
1
- /**
2
- * A symbol used as the default value for contexts that require a provider. When
3
- * calling useContext, we can throw an error if we find this symbol rather than
4
- * a provided value. This is useful in cases where we want to consider null or
5
- * undefined as valid provided values.
6
- */
7
- export declare const NO_PROVIDER: unique symbol;
8
- export type NoProvider = typeof NO_PROVIDER;
@@ -1,7 +0,0 @@
1
- /**
2
- * A symbol used as the default value for contexts that require a provider. When
3
- * calling useContext, we can throw an error if we find this symbol rather than
4
- * a provided value. This is useful in cases where we want to consider null or
5
- * undefined as valid provided values.
6
- */
7
- export const NO_PROVIDER = Symbol('no provider');
@@ -1,26 +0,0 @@
1
- import type { ElementsInstance } from '@recurly/recurly-js';
2
- import { PropsWithChildren } from 'react';
3
- /**
4
- * A provider that provides the Elements instance via context.
5
- *
6
- * If used outside of a RecurlyProvider, it will throw an error.
7
- */
8
- export declare function Elements({ children }: PropsWithChildren): import("react/jsx-runtime").JSX.Element;
9
- /**
10
- * A hook that returns the Elements instance from context.
11
- *
12
- * If used outside of an Elements provider, it will throw an error.
13
- *
14
- * Within an Elements provider:
15
- * - If used on the server, it will return undefined.
16
- * - If used on the client, it will initially return undefined and then return
17
- * the provided Elements instance once the Recurly library is loaded.
18
- *
19
- * @param options - An object to provide an alternate contextual error message
20
- * to use if one tries to use this hook outside of an Elements provider.
21
- *
22
- * @returns The Elements instance.
23
- */
24
- export declare function useElements(options?: {
25
- noProviderError?: string;
26
- }): ElementsInstance | undefined;
@@ -1,46 +0,0 @@
1
- import { createContext, useContext, useMemo } from 'react';
2
- import { NO_PROVIDER } from '../NoProvider';
3
- import { useRecurly } from '../recurly';
4
- import { jsx as _jsx } from "react/jsx-runtime";
5
- const ElementCtx = /*#__PURE__*/createContext(NO_PROVIDER);
6
-
7
- /**
8
- * A provider that provides the Elements instance via context.
9
- *
10
- * If used outside of a RecurlyProvider, it will throw an error.
11
- */
12
- export function Elements({
13
- children
14
- }) {
15
- const recurly = useRecurly({
16
- noProviderError: 'Elements must be used within a RecurlyProvider.'
17
- });
18
- const elements = useMemo(() => recurly?.Elements(), [recurly]);
19
- return /*#__PURE__*/_jsx(ElementCtx.Provider, {
20
- value: elements,
21
- children: children
22
- });
23
- }
24
-
25
- /**
26
- * A hook that returns the Elements instance from context.
27
- *
28
- * If used outside of an Elements provider, it will throw an error.
29
- *
30
- * Within an Elements provider:
31
- * - If used on the server, it will return undefined.
32
- * - If used on the client, it will initially return undefined and then return
33
- * the provided Elements instance once the Recurly library is loaded.
34
- *
35
- * @param options - An object to provide an alternate contextual error message
36
- * to use if one tries to use this hook outside of an Elements provider.
37
- *
38
- * @returns The Elements instance.
39
- */
40
- export function useElements(options) {
41
- const elements = useContext(ElementCtx);
42
- if (elements === NO_PROVIDER) {
43
- throw new Error(options?.noProviderError ?? 'useElements must be used within a Elements provider.');
44
- }
45
- return elements;
46
- }
@@ -1,88 +0,0 @@
1
- import { setupRtl } from '@codecademy/gamut-tests';
2
- import { RecurlyProvider } from '../recurly';
3
- import { Elements, useElements } from '.';
4
- import { jsx as _jsx } from "react/jsx-runtime";
5
- let mockRecurlyPromise;
6
- jest.mock('../recurly/loadRecurly', () => ({
7
- loadRecurly: () => mockRecurlyPromise
8
- }));
9
- const renderView = setupRtl(({
10
- children
11
- }) => children);
12
- const jsEventLoop = () => new Promise(r => setTimeout(r, 0));
13
- describe('Elements', () => {
14
- it('should show content before recurly loads', () => {
15
- mockRecurlyPromise = new Promise(() => {
16
- // never resolve
17
- });
18
- const {
19
- view
20
- } = renderView({
21
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
22
- publicKey: "testKey",
23
- required: ['cvv', 'postal_code'],
24
- children: /*#__PURE__*/_jsx(Elements, {
25
- children: "test-content"
26
- })
27
- })
28
- });
29
- expect(view.getByText('test-content')).toBeInTheDocument();
30
- });
31
- it('should call Elements on recurly', async () => {
32
- const mockRecurly = {
33
- Elements: jest.fn()
34
- };
35
- mockRecurlyPromise = Promise.resolve(mockRecurly);
36
- renderView({
37
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
38
- publicKey: "testKey",
39
- required: ['cvv', 'postal_code'],
40
- children: /*#__PURE__*/_jsx(Elements, {})
41
- })
42
- });
43
- await jsEventLoop(); // one for the Provider's useEffect to run
44
- await jsEventLoop(); // one for the subsequent re-render where useMemo runs
45
- expect(mockRecurly.Elements).toHaveBeenCalled();
46
- });
47
- });
48
- describe('useElements', () => {
49
- const Consumer = () => {
50
- try {
51
- const elements = useElements();
52
- return /*#__PURE__*/_jsx("div", {
53
- children: typeof elements
54
- });
55
- } catch {
56
- return /*#__PURE__*/_jsx("div", {
57
- children: "error"
58
- });
59
- }
60
- };
61
- it('throws an error when used without a Elements provider', () => {
62
- const {
63
- view
64
- } = renderView({
65
- children: /*#__PURE__*/_jsx(Consumer, {})
66
- });
67
- expect(view.getByText('error')).toBeInTheDocument();
68
- });
69
- it('returns null initially, then an Elements instance once Recurly loads', async () => {
70
- const mockRecurly = {
71
- Elements: () => ({})
72
- };
73
- mockRecurlyPromise = Promise.resolve(mockRecurly);
74
- const {
75
- view
76
- } = renderView({
77
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
78
- publicKey: "testKey",
79
- required: ['cvv', 'postal_code'],
80
- children: /*#__PURE__*/_jsx(Elements, {
81
- children: /*#__PURE__*/_jsx(Consumer, {})
82
- })
83
- })
84
- });
85
- expect(view.getByText('undefined')).toBeInTheDocument();
86
- expect(await view.findByText('object')).toBeInTheDocument();
87
- });
88
- });
@@ -1,2 +0,0 @@
1
- export * from './recurly';
2
- export * from './elements';
@@ -1,2 +0,0 @@
1
- export * from './recurly';
2
- export * from './elements';
@@ -1,30 +0,0 @@
1
- import type { Recurly } from '@recurly/recurly-js';
2
- import { PropsWithChildren } from 'react';
3
- import { RecurlyConfig } from './loadRecurly';
4
- export type RecurlyProviderProps = PropsWithChildren<RecurlyConfig>;
5
- /**
6
- * A provider that:
7
- * - loads the Recurly library
8
- * - ensures that only a single instance of the library is loaded
9
- * - configures the library with the provided public key
10
- * - provides the library instance via context
11
- */
12
- export declare function RecurlyProvider({ children, ...config }: RecurlyProviderProps): import("react/jsx-runtime").JSX.Element;
13
- /**
14
- * A hook that returns the Recurly instance from context.
15
- *
16
- * If used outside of a RecurlyProvider, it will throw an error.
17
- *
18
- * Within a RecurlyProvider:
19
- * - If used on the server, it will return undefined.
20
- * - If used on the client, it will initially return undefined and then return
21
- * the provided Recurly instance once it is loaded.
22
- *
23
- * @param options - An object to provide an alternate contextual error message
24
- * to use if one tries to use this hook outside of a RecurlyProvider.
25
- *
26
- * @returns The Recurly instance.
27
- */
28
- export declare function useRecurly(options?: {
29
- noProviderError?: string;
30
- }): Recurly | undefined;
@@ -1,50 +0,0 @@
1
- import { createContext, useContext, useEffect, useState } from 'react';
2
- import { NO_PROVIDER } from '../NoProvider';
3
- import { loadRecurly } from './loadRecurly';
4
- import { jsx as _jsx } from "react/jsx-runtime";
5
- const RecurlyCtx = /*#__PURE__*/createContext(NO_PROVIDER);
6
- /**
7
- * A provider that:
8
- * - loads the Recurly library
9
- * - ensures that only a single instance of the library is loaded
10
- * - configures the library with the provided public key
11
- * - provides the library instance via context
12
- */
13
- export function RecurlyProvider({
14
- children,
15
- ...config
16
- }) {
17
- const [recurly, setRecurly] = useState(undefined);
18
- useEffect(() => {
19
- loadRecurly(config).then(setRecurly);
20
- // Exclude `config` dependency as it should be treated as static.
21
- // eslint-disable-next-line react-hooks/exhaustive-deps
22
- }, []);
23
- return /*#__PURE__*/_jsx(RecurlyCtx.Provider, {
24
- value: recurly,
25
- children: children
26
- });
27
- }
28
-
29
- /**
30
- * A hook that returns the Recurly instance from context.
31
- *
32
- * If used outside of a RecurlyProvider, it will throw an error.
33
- *
34
- * Within a RecurlyProvider:
35
- * - If used on the server, it will return undefined.
36
- * - If used on the client, it will initially return undefined and then return
37
- * the provided Recurly instance once it is loaded.
38
- *
39
- * @param options - An object to provide an alternate contextual error message
40
- * to use if one tries to use this hook outside of a RecurlyProvider.
41
- *
42
- * @returns The Recurly instance.
43
- */
44
- export function useRecurly(options) {
45
- const recurly = useContext(RecurlyCtx);
46
- if (recurly === NO_PROVIDER) {
47
- throw new Error(options?.noProviderError ?? 'useRecurly must be used within a RecurlyProvider');
48
- }
49
- return recurly;
50
- }
@@ -1,63 +0,0 @@
1
- import { setupRtl } from '@codecademy/gamut-tests';
2
- import { RecurlyProvider, useRecurly } from '.';
3
- import { jsx as _jsx } from "react/jsx-runtime";
4
- let mockRecurlyPromise;
5
- jest.mock('./loadRecurly', () => ({
6
- loadRecurly: () => mockRecurlyPromise
7
- }));
8
- describe('RecurlyProvider', () => {
9
- const renderView = setupRtl(RecurlyProvider);
10
- it('should show content before recurly loads', () => {
11
- mockRecurlyPromise = new Promise(() => {
12
- // never resolve
13
- });
14
- const {
15
- view
16
- } = renderView({
17
- publicKey: 'testKey',
18
- required: ['cvv', 'postal_code'],
19
- children: 'test-content'
20
- });
21
- expect(view.getByText('test-content')).toBeInTheDocument();
22
- });
23
- });
24
- describe('useRecurly', () => {
25
- const renderView = setupRtl(({
26
- children
27
- }) => children);
28
- const Consumer = () => {
29
- try {
30
- const recurly = useRecurly();
31
- return /*#__PURE__*/_jsx("div", {
32
- children: typeof recurly
33
- });
34
- } catch {
35
- return /*#__PURE__*/_jsx("div", {
36
- children: "error"
37
- });
38
- }
39
- };
40
- it('throws an error when used without a RecurlyProvider', () => {
41
- mockRecurlyPromise = Promise.resolve({});
42
- const {
43
- view
44
- } = renderView({
45
- children: /*#__PURE__*/_jsx(Consumer, {})
46
- });
47
- expect(view.getByText('error')).toBeInTheDocument();
48
- });
49
- it('returns undefined and then an instance when used inside a RecurlyProvider', async () => {
50
- mockRecurlyPromise = Promise.resolve({});
51
- const {
52
- view
53
- } = renderView({
54
- children: /*#__PURE__*/_jsx(RecurlyProvider, {
55
- publicKey: "testKey",
56
- required: ['cvv', 'postal_code'],
57
- children: /*#__PURE__*/_jsx(Consumer, {})
58
- })
59
- });
60
- expect(view.getByText('undefined')).toBeInTheDocument();
61
- expect(await view.findByText('object')).toBeInTheDocument();
62
- });
63
- });
@@ -1,6 +0,0 @@
1
- import type { Recurly } from '@recurly/recurly-js';
2
- export type RecurlyConfig = {
3
- publicKey: string;
4
- required: string[];
5
- };
6
- export declare function loadRecurly(config: RecurlyConfig): Promise<Recurly>;
@@ -1,22 +0,0 @@
1
- import { w } from './w';
2
- export function loadRecurly(config) {
3
- if (!w._recurlyPromise) {
4
- w._recurlyPromise = new Promise(resolve => {
5
- const script = w.document.createElement('script');
6
- script.src = 'https://js.recurly.com/v4/recurly.js';
7
- script.async = true;
8
- script.onload = () => {
9
- const {
10
- recurly
11
- } = w;
12
- if (!recurly) {
13
- throw new Error('Recurly was not loaded by recurly.js');
14
- }
15
- recurly.configure(config);
16
- resolve(recurly);
17
- };
18
- w.document.head.appendChild(script);
19
- });
20
- }
21
- return w._recurlyPromise;
22
- }