@ndla/primitives 0.0.1

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 (97) hide show
  1. package/LICENSE +674 -0
  2. package/dist/panda.buildinfo.json +426 -0
  3. package/dist/styles.css +1738 -0
  4. package/es/Accordion.js +103 -0
  5. package/es/ArticleLists.js +106 -0
  6. package/es/Badge.js +55 -0
  7. package/es/BlockQuote.js +49 -0
  8. package/es/Button.js +211 -0
  9. package/es/Checkbox.js +118 -0
  10. package/es/Dialog.js +329 -0
  11. package/es/ExpandableBox.js +55 -0
  12. package/es/FieldErrorMessage.js +44 -0
  13. package/es/FieldHelper.js +37 -0
  14. package/es/FormControl.js +163 -0
  15. package/es/FramedContent.js +54 -0
  16. package/es/Icon.js +71 -0
  17. package/es/Input.js +159 -0
  18. package/es/Label.js +104 -0
  19. package/es/Menu.js +171 -0
  20. package/es/MessageBox.js +57 -0
  21. package/es/NdlaLogo.js +284 -0
  22. package/es/Pagination.js +37 -0
  23. package/es/Popover.js +78 -0
  24. package/es/RadioGroup.js +136 -0
  25. package/es/Skeleton.js +31 -0
  26. package/es/Slider.js +102 -0
  27. package/es/Spinner.js +54 -0
  28. package/es/Switch.js +130 -0
  29. package/es/Table.js +75 -0
  30. package/es/Text.js +54 -0
  31. package/es/Toast.js +82 -0
  32. package/es/Tooltip.js +59 -0
  33. package/es/createStyleContext.js +62 -0
  34. package/es/index.js +19 -0
  35. package/lib/Accordion.d.ts +17 -0
  36. package/lib/Accordion.js +109 -0
  37. package/lib/ArticleLists.d.ts +20 -0
  38. package/lib/ArticleLists.js +115 -0
  39. package/lib/Badge.d.ts +33 -0
  40. package/lib/Badge.js +62 -0
  41. package/lib/BlockQuote.d.ts +28 -0
  42. package/lib/BlockQuote.js +56 -0
  43. package/lib/Button.d.ts +131 -0
  44. package/lib/Button.js +217 -0
  45. package/lib/Checkbox.d.ts +15 -0
  46. package/lib/Checkbox.js +125 -0
  47. package/lib/Dialog.d.ts +107 -0
  48. package/lib/Dialog.js +338 -0
  49. package/lib/ExpandableBox.d.ts +12 -0
  50. package/lib/ExpandableBox.js +63 -0
  51. package/lib/FieldErrorMessage.d.ts +11 -0
  52. package/lib/FieldErrorMessage.js +50 -0
  53. package/lib/FieldHelper.d.ts +11 -0
  54. package/lib/FieldHelper.js +43 -0
  55. package/lib/FormControl.d.ts +65 -0
  56. package/lib/FormControl.js +173 -0
  57. package/lib/FramedContent.d.ts +32 -0
  58. package/lib/FramedContent.js +61 -0
  59. package/lib/Icon.d.ts +37 -0
  60. package/lib/Icon.js +78 -0
  61. package/lib/Input.d.ts +20 -0
  62. package/lib/Input.js +165 -0
  63. package/lib/Label.d.ts +16 -0
  64. package/lib/Label.js +110 -0
  65. package/lib/Menu.d.ts +25 -0
  66. package/lib/Menu.js +179 -0
  67. package/lib/MessageBox.d.ts +33 -0
  68. package/lib/MessageBox.js +64 -0
  69. package/lib/NdlaLogo.d.ts +15 -0
  70. package/lib/NdlaLogo.js +293 -0
  71. package/lib/Pagination.d.ts +14 -0
  72. package/lib/Pagination.js +43 -0
  73. package/lib/Popover.d.ts +22 -0
  74. package/lib/Popover.js +87 -0
  75. package/lib/RadioGroup.d.ts +19 -0
  76. package/lib/RadioGroup.js +143 -0
  77. package/lib/Skeleton.d.ts +11 -0
  78. package/lib/Skeleton.js +38 -0
  79. package/lib/Slider.d.ts +17 -0
  80. package/lib/Slider.js +109 -0
  81. package/lib/Spinner.d.ts +26 -0
  82. package/lib/Spinner.js +61 -0
  83. package/lib/Switch.d.ts +21 -0
  84. package/lib/Switch.js +137 -0
  85. package/lib/Table.d.ts +10 -0
  86. package/lib/Table.js +82 -0
  87. package/lib/Text.d.ts +24 -0
  88. package/lib/Text.js +62 -0
  89. package/lib/Toast.d.ts +18 -0
  90. package/lib/Toast.js +90 -0
  91. package/lib/Tooltip.d.ts +16 -0
  92. package/lib/Tooltip.js +65 -0
  93. package/lib/createStyleContext.d.ts +27 -0
  94. package/lib/createStyleContext.js +69 -0
  95. package/lib/index.d.ts +28 -0
  96. package/lib/index.js +130 -0
  97. package/package.json +48 -0
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useFormControlProps = exports.useFormControlContext = exports.useFormControl = exports.FormControl = void 0;
7
+ var _react = require("react");
8
+ var _jsx2 = require("@ndla/styled-system/jsx");
9
+ var _util = require("@ndla/util");
10
+ var _jsxRuntime = require("react/jsx-runtime");
11
+ /**
12
+ * Copyright (c) 2024-present, NDLA.
13
+ *
14
+ * This source code is licensed under the GPLv3 license found in the
15
+ * LICENSE file in the root directory of this source tree.
16
+ *
17
+ */
18
+
19
+ const StyledFormControl = (0, _jsx2.styled)("div", {
20
+ base: {
21
+ display: "flex",
22
+ flexDirection: "column",
23
+ gap: "3xsmall"
24
+ }
25
+ });
26
+ const useFormControlProvider = _ref => {
27
+ let {
28
+ id: idProp,
29
+ isRequired,
30
+ isDisabled,
31
+ isInvalid
32
+ } = _ref;
33
+ const id = "field-".concat(idProp);
34
+ const labelId = "".concat(id, "-label");
35
+ const errorTextId = "".concat(id, "-error-message");
36
+ const helpTextId = "".concat(id, "-helper");
37
+ const [hasErrorText, setHasErrorText] = (0, _react.useState)(false);
38
+ const [hasHelpText, setHasHelpText] = (0, _react.useState)(false);
39
+ const getHelpTextProps = (0, _react.useCallback)(function () {
40
+ let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
41
+ let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
42
+ return {
43
+ id: helpTextId,
44
+ ...props,
45
+ ref: (0, _util.composeRefs)(forwardedRef, node => {
46
+ if (!node) return;
47
+ setHasHelpText(true);
48
+ })
49
+ };
50
+ }, [helpTextId]);
51
+ const getLabelProps = (0, _react.useCallback)(function () {
52
+ let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
53
+ let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
54
+ return {
55
+ ...props,
56
+ ref: forwardedRef,
57
+ "data-disabled": props !== null && props !== void 0 && props["data-disabled"] ? props["data-disabled"] : isDisabled,
58
+ "data-invalid": props !== null && props !== void 0 && props["data-invalid"] ? props["data-invalid"] : isInvalid,
59
+ id: props.id !== undefined ? props.id : labelId,
60
+ htmlFor: props.htmlFor !== undefined ? props.htmlFor : id
61
+ };
62
+ }, [id, isDisabled, isInvalid, labelId]);
63
+ const getErrorMessageProps = (0, _react.useCallback)(function () {
64
+ let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
65
+ let forwardedRef = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
66
+ return {
67
+ id: errorTextId,
68
+ ...props,
69
+ ref: (0, _util.composeRefs)(forwardedRef, node => {
70
+ if (!node) return;
71
+ setHasErrorText(true);
72
+ }),
73
+ "aria-live": "polite"
74
+ };
75
+ }, [errorTextId]);
76
+ return {
77
+ isRequired: !!isRequired,
78
+ isDisabled: !!isDisabled,
79
+ isInvalid: !!isInvalid,
80
+ hasErrorText,
81
+ setHasErrorText,
82
+ hasHelpText,
83
+ setHasHelpText,
84
+ id,
85
+ labelId,
86
+ errorTextId,
87
+ helpTextId,
88
+ getHelpTextProps,
89
+ getErrorMessageProps,
90
+ getLabelProps
91
+ };
92
+ };
93
+ const FormControlContext = /*#__PURE__*/(0, _react.createContext)(undefined);
94
+
95
+ /**
96
+ * Form control component inspired by chakra-ui. Allows for easy composition of form fields.
97
+ */
98
+ const FormControl = _ref2 => {
99
+ let {
100
+ children,
101
+ id,
102
+ isDisabled,
103
+ isInvalid,
104
+ isRequired,
105
+ ...rest
106
+ } = _ref2;
107
+ const context = useFormControlProvider({
108
+ id,
109
+ isDisabled,
110
+ isInvalid,
111
+ isRequired
112
+ });
113
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(FormControlContext.Provider, {
114
+ value: context,
115
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledFormControl, {
116
+ ...rest,
117
+ children: children
118
+ })
119
+ });
120
+ };
121
+ exports.FormControl = FormControl;
122
+ const useFormControlContext = () => {
123
+ const context = (0, _react.useContext)(FormControlContext);
124
+ return context;
125
+ };
126
+ exports.useFormControlContext = useFormControlContext;
127
+ const useFormControlProps = _ref3 => {
128
+ let {
129
+ id,
130
+ disabled,
131
+ required,
132
+ isDisabled,
133
+ isInvalid,
134
+ isRequired,
135
+ ...rest
136
+ } = _ref3;
137
+ const field = useFormControlContext();
138
+ const labelIds = [];
139
+ if (field !== null && field !== void 0 && field.hasErrorText && field !== null && field !== void 0 && field.isInvalid) {
140
+ labelIds.push(field.errorTextId);
141
+ }
142
+ if (rest["aria-describedby"]) {
143
+ labelIds.push(rest["aria-describedby"]);
144
+ }
145
+ if (field !== null && field !== void 0 && field.hasHelpText) {
146
+ labelIds.push(field.helpTextId);
147
+ }
148
+ return {
149
+ ...rest,
150
+ "aria-describedby": labelIds.join(" ") || undefined,
151
+ id: id !== null && id !== void 0 ? id : field === null || field === void 0 ? void 0 : field.id,
152
+ isDisabled: disabled !== null && disabled !== void 0 ? disabled : field === null || field === void 0 ? void 0 : field.isDisabled,
153
+ isRequired: required !== null && required !== void 0 ? required : field === null || field === void 0 ? void 0 : field.isRequired,
154
+ isInvalid: isInvalid !== null && isInvalid !== void 0 ? isInvalid : field === null || field === void 0 ? void 0 : field.isInvalid
155
+ };
156
+ };
157
+ exports.useFormControlProps = useFormControlProps;
158
+ const useFormControl = props => {
159
+ const {
160
+ isDisabled,
161
+ isInvalid,
162
+ isRequired,
163
+ ...rest
164
+ } = useFormControlProps(props);
165
+ return {
166
+ ...rest,
167
+ disabled: isDisabled,
168
+ required: isRequired,
169
+ "aria-invalid": isInvalid ? true : undefined,
170
+ "aria-required": isRequired ? true : undefined
171
+ };
172
+ };
173
+ exports.useFormControl = useFormControl;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) 2024-present, NDLA.
3
+ *
4
+ * This source code is licensed under the GPLv3 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import { ComponentPropsWithoutRef } from "react";
9
+ import { RecipeVariantProps } from "@ndla/styled-system/types";
10
+ declare const framedContentRecipe: import("@ndla/styled-system/types").RecipeRuntimeFn<{
11
+ colorTheme: {
12
+ neutral: {
13
+ backgroundColor: "surface.default";
14
+ borderColor: "stroke.subtle";
15
+ boxShadowColor: "stroke.subtle";
16
+ };
17
+ brand1: {
18
+ backgroundColor: "surface.brand.1.subtle";
19
+ borderColor: "surface.brand.1.strong";
20
+ boxShadowColor: "surface.brand.1.strong";
21
+ };
22
+ brand2: {
23
+ backgroundColor: "surface.brand.2.subtle";
24
+ borderColor: "surface.brand.2.strong";
25
+ boxShadowColor: "surface.brand.2.strong";
26
+ };
27
+ };
28
+ }>;
29
+ export type FramedContentVariantProps = RecipeVariantProps<typeof framedContentRecipe>;
30
+ export type FramedContentProps = ComponentPropsWithoutRef<"div"> & FramedContentVariantProps;
31
+ export declare const FramedContent: ({ className, colorTheme, ...rest }: FramedContentProps) => import("@emotion/react/jsx-runtime").JSX.Element;
32
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.FramedContent = void 0;
7
+ var _css = require("@ndla/styled-system/css");
8
+ var _jsx2 = require("@ndla/styled-system/jsx");
9
+ var _jsxRuntime = require("react/jsx-runtime");
10
+ /**
11
+ * Copyright (c) 2024-present, NDLA.
12
+ *
13
+ * This source code is licensed under the GPLv3 license found in the
14
+ * LICENSE file in the root directory of this source tree.
15
+ *
16
+ */
17
+
18
+ const framedContentRecipe = (0, _css.cva)({
19
+ base: {
20
+ padding: "medium",
21
+ border: "1px solid",
22
+ borderRadius: "small",
23
+ boxShadow: "4px 4px 0px 0px var(--shadow-color)"
24
+ },
25
+ variants: {
26
+ colorTheme: {
27
+ neutral: {
28
+ backgroundColor: "surface.default",
29
+ borderColor: "stroke.subtle",
30
+ boxShadowColor: "stroke.subtle"
31
+ },
32
+ brand1: {
33
+ backgroundColor: "surface.brand.1.subtle",
34
+ borderColor: "surface.brand.1.strong",
35
+ boxShadowColor: "surface.brand.1.strong"
36
+ },
37
+ brand2: {
38
+ backgroundColor: "surface.brand.2.subtle",
39
+ borderColor: "surface.brand.2.strong",
40
+ boxShadowColor: "surface.brand.2.strong"
41
+ }
42
+ }
43
+ },
44
+ defaultVariants: {
45
+ colorTheme: "neutral"
46
+ }
47
+ });
48
+ const FramedContent = _ref => {
49
+ let {
50
+ className,
51
+ colorTheme,
52
+ ...rest
53
+ } = _ref;
54
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsx2.styled.div, {
55
+ className: (0, _css.cx)(framedContentRecipe({
56
+ colorTheme
57
+ }), className),
58
+ ...rest
59
+ });
60
+ };
61
+ exports.FramedContent = FramedContent;
package/lib/Icon.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) 2024-present, NDLA.
3
+ *
4
+ * This source code is licensed under the GPLv3 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import { ComponentPropsWithRef } from "react";
9
+ import { type RecipeVariantProps } from "@ndla/styled-system/css";
10
+ export declare const iconRecipe: import("@ndla/styled-system/types").RecipeRuntimeFn<{
11
+ size: {
12
+ xsmall: {
13
+ width: "xsmall";
14
+ height: "xsmall";
15
+ };
16
+ small: {
17
+ width: "small";
18
+ height: "small";
19
+ };
20
+ medium: {
21
+ width: "medium";
22
+ height: "medium";
23
+ };
24
+ large: {
25
+ width: "large";
26
+ height: "large";
27
+ };
28
+ };
29
+ }>;
30
+ export type IconVariantProps = RecipeVariantProps<typeof iconRecipe>;
31
+ interface BaseIconProps extends ComponentPropsWithRef<"svg"> {
32
+ title?: string;
33
+ description?: string;
34
+ }
35
+ export type IconProps = BaseIconProps & IconVariantProps;
36
+ export declare const Icon: ({ children, size, role, title, description, width, height, className, "aria-hidden": ariaHidden, ...props }: IconProps) => import("@emotion/react/jsx-runtime").JSX.Element;
37
+ export {};
package/lib/Icon.js ADDED
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.iconRecipe = exports.Icon = void 0;
7
+ var _css = require("@ndla/styled-system/css");
8
+ var _jsxRuntime = require("react/jsx-runtime");
9
+ /**
10
+ * Copyright (c) 2024-present, NDLA.
11
+ *
12
+ * This source code is licensed under the GPLv3 license found in the
13
+ * LICENSE file in the root directory of this source tree.
14
+ *
15
+ */
16
+
17
+ const iconRecipe = exports.iconRecipe = (0, _css.cva)({
18
+ base: {
19
+ display: "inline-block",
20
+ fill: "currentcolor",
21
+ verticalAlign: "middle",
22
+ lineHeight: "1em",
23
+ flexShrink: "0"
24
+ },
25
+ defaultVariants: {
26
+ size: "medium"
27
+ },
28
+ variants: {
29
+ size: {
30
+ xsmall: {
31
+ width: "xsmall",
32
+ height: "xsmall"
33
+ },
34
+ small: {
35
+ width: "small",
36
+ height: "small"
37
+ },
38
+ medium: {
39
+ width: "medium",
40
+ height: "medium"
41
+ },
42
+ large: {
43
+ width: "large",
44
+ height: "large"
45
+ }
46
+ }
47
+ }
48
+ });
49
+ // TODO: Move this component over to ndla/icons
50
+ const Icon = _ref => {
51
+ let {
52
+ children,
53
+ size,
54
+ role,
55
+ title,
56
+ description,
57
+ width,
58
+ height,
59
+ className,
60
+ "aria-hidden": ariaHidden = true,
61
+ ...props
62
+ } = _ref;
63
+ return /*#__PURE__*/(0, _jsxRuntime.jsxs)("svg", {
64
+ "data-icon": "",
65
+ "aria-hidden": ariaHidden,
66
+ preserveAspectRatio: "xMidYMid meet",
67
+ ...props,
68
+ className: (0, _css.cx)(iconRecipe({
69
+ size
70
+ }), className),
71
+ children: [title && /*#__PURE__*/(0, _jsxRuntime.jsx)("title", {
72
+ children: title
73
+ }), description && /*#__PURE__*/(0, _jsxRuntime.jsx)("desc", {
74
+ children: description
75
+ }), children]
76
+ });
77
+ };
78
+ exports.Icon = Icon;
package/lib/Input.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) 2024-present, NDLA.
3
+ *
4
+ * This source code is licensed under the GPLv3 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import { ComponentPropsWithRef, HTMLAttributes } from "react";
9
+ export declare const InputContainer: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
10
+ export declare const Input: import("react").ForwardRefExoticComponent<Omit<Omit<import("react").DetailedHTMLProps<import("react").InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "ref"> & {
11
+ ref?: ((instance: HTMLInputElement | null) => void) | import("react").RefObject<HTMLInputElement> | null | undefined;
12
+ }, "ref"> & import("react").RefAttributes<HTMLInputElement>>;
13
+ export declare const FormInput: import("react").ForwardRefExoticComponent<Omit<Omit<import("react").DetailedHTMLProps<import("react").InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>, "ref"> & {
14
+ ref?: ((instance: HTMLInputElement | null) => void) | import("react").RefObject<HTMLInputElement> | null | undefined;
15
+ }, "ref"> & import("react").RefAttributes<HTMLInputElement>>;
16
+ interface TextAreaProps extends ComponentPropsWithRef<"textarea"> {
17
+ }
18
+ export declare const TextArea: import("react").ForwardRefExoticComponent<Omit<TextAreaProps, "ref"> & import("react").RefAttributes<HTMLTextAreaElement>>;
19
+ export declare const FormTextArea: import("react").ForwardRefExoticComponent<Omit<TextAreaProps, "ref"> & import("react").RefAttributes<HTMLTextAreaElement>>;
20
+ export {};
package/lib/Input.js ADDED
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TextArea = exports.InputContainer = exports.Input = exports.FormTextArea = exports.FormInput = void 0;
7
+ var _react = require("react");
8
+ var _css = require("@ndla/styled-system/css");
9
+ var _jsx2 = require("@ndla/styled-system/jsx");
10
+ var _util = require("@ndla/util");
11
+ var _FormControl = require("./FormControl");
12
+ var _jsxRuntime = require("react/jsx-runtime");
13
+ /**
14
+ * Copyright (c) 2024-present, NDLA.
15
+ *
16
+ * This source code is licensed under the GPLv3 license found in the
17
+ * LICENSE file in the root directory of this source tree.
18
+ *
19
+ */
20
+
21
+ const InputContext = /*#__PURE__*/(0, _react.createContext)(undefined);
22
+ const inputCss = _css.css.raw({
23
+ outline: "1px solid",
24
+ outlineColor: "stroke.subtle",
25
+ background: "background.default",
26
+ borderRadius: "xsmall",
27
+ minHeight: "xxlarge",
28
+ paddingBlock: "0",
29
+ paddingInline: "xsmall",
30
+ _ariaInvalid: {
31
+ outlineColor: "stroke.error",
32
+ _hover: {
33
+ outlineColor: "stroke.error"
34
+ },
35
+ _focusWithin: {
36
+ outlineColor: "stroke.error",
37
+ _hover: {
38
+ outlineColor: "stroke.error"
39
+ }
40
+ }
41
+ },
42
+ _hover: {
43
+ outlineColor: "stroke.hover"
44
+ },
45
+ _focusWithin: {
46
+ outlineWidth: "2px",
47
+ outlineOffset: "-1px",
48
+ outlineColor: "stroke.default",
49
+ _hover: {
50
+ outlineColor: "stroke.default"
51
+ }
52
+ },
53
+ "&:disabled, &:has(:disabled)": {
54
+ outlineColor: "stroke.subtle",
55
+ _hover: {
56
+ outlineColor: "stroke.subtle"
57
+ }
58
+ }
59
+ });
60
+ const StyledInputContainer = (0, _jsx2.styled)("div", {
61
+ base: {
62
+ width: "100%",
63
+ display: "flex",
64
+ alignItems: "center",
65
+ "& svg": {
66
+ width: "medium",
67
+ height: "medium"
68
+ }
69
+ }
70
+ });
71
+ const InputContainer = exports.InputContainer = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
72
+ let {
73
+ children,
74
+ className,
75
+ ...rest
76
+ } = _ref;
77
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(InputContext.Provider, {
78
+ value: {},
79
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledInputContainer, {
80
+ className: (0, _css.cx)((0, _css.css)(inputCss), className),
81
+ ...rest,
82
+ ref: ref,
83
+ children: children
84
+ })
85
+ });
86
+ });
87
+ const baseInputCss = _css.css.raw({
88
+ width: "100%",
89
+ color: "text.default",
90
+ outline: "none",
91
+ background: "none",
92
+ border: "0",
93
+ padding: "xxsmall",
94
+ _disabled: {
95
+ cursor: "not-allowed"
96
+ },
97
+ // Chrome messes with input styling when autofilling. Having a really long transitions cancels this out.
98
+ _autofill: {
99
+ transition: "background-color 600000s 0s, color 600000s 0s"
100
+ },
101
+ _focus: {
102
+ appearance: "none"
103
+ }
104
+ });
105
+ const baseTextAreaCss = _css.css.raw({
106
+ paddingInline: "xsmall",
107
+ paddingBlock: "xsmall",
108
+ resize: "none",
109
+ overflowY: "hidden"
110
+ });
111
+ const Input = exports.Input = /*#__PURE__*/(0, _react.forwardRef)((_ref2, ref) => {
112
+ let {
113
+ className,
114
+ ...props
115
+ } = _ref2;
116
+ const context = (0, _react.useContext)(InputContext);
117
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsx2.styled.input, {
118
+ className: (0, _css.cx)((0, _css.css)(baseInputCss, context ? undefined : inputCss), className),
119
+ ref: ref,
120
+ ...props
121
+ });
122
+ });
123
+ const FormInput = exports.FormInput = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
124
+ const field = (0, _FormControl.useFormControl)(props);
125
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(Input, {
126
+ ...field,
127
+ ref: ref
128
+ });
129
+ });
130
+ const TextArea = exports.TextArea = /*#__PURE__*/(0, _react.forwardRef)((_ref3, ref) => {
131
+ let {
132
+ className,
133
+ ...props
134
+ } = _ref3;
135
+ const context = (0, _react.useContext)(InputContext);
136
+ const localRef = (0, _react.useRef)(null);
137
+
138
+ // Automatically resize a textarea based on its content
139
+ const resize = (0, _react.useCallback)(() => {
140
+ if (!localRef.current) return;
141
+ localRef.current.style.height = "0";
142
+ localRef.current.style.height = "".concat(localRef.current.scrollHeight + 3, "px");
143
+ }, []);
144
+ (0, _react.useEffect)(() => {
145
+ window.addEventListener("input", resize);
146
+ window.addEventListener("resize", resize);
147
+ resize();
148
+ return () => {
149
+ window.removeEventListener("input", resize);
150
+ window.removeEventListener("resize", resize);
151
+ };
152
+ }, [resize]);
153
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsx2.styled.textarea, {
154
+ className: (0, _css.cx)((0, _css.css)(baseInputCss, context ? undefined : inputCss, baseTextAreaCss), className),
155
+ ref: (0, _util.composeRefs)(ref, localRef),
156
+ ...props
157
+ });
158
+ });
159
+ const FormTextArea = exports.FormTextArea = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
160
+ const field = (0, _FormControl.useFormControl)(props);
161
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(TextArea, {
162
+ ...field,
163
+ ref: ref
164
+ });
165
+ });
package/lib/Label.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) 2024-present, NDLA.
3
+ *
4
+ * This source code is licensed under the GPLv3 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+ import { ComponentPropsWithRef } from "react";
9
+ import { TextProps } from "./Text";
10
+ export type LegendProps = ComponentPropsWithRef<"legend"> & TextProps;
11
+ export declare const Legend: import("react").ForwardRefExoticComponent<Omit<LegendProps, "ref"> & import("react").RefAttributes<HTMLLegendElement>>;
12
+ export declare const FormLegend: import("react").ForwardRefExoticComponent<Omit<LegendProps, "ref"> & import("react").RefAttributes<HTMLLegendElement>>;
13
+ export type LabelProps = ComponentPropsWithRef<"label"> & TextProps;
14
+ export declare const Label: import("react").ForwardRefExoticComponent<Omit<LabelProps, "ref"> & import("react").RefAttributes<HTMLLabelElement>>;
15
+ export declare const FormLabel: import("react").ForwardRefExoticComponent<Omit<LabelProps, "ref"> & import("react").RefAttributes<HTMLLabelElement>>;
16
+ export declare const Fieldset: import("@ndla/styled-system/jsx").StyledComponent<"fieldset", {}>;
package/lib/Label.js ADDED
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Legend = exports.Label = exports.FormLegend = exports.FormLabel = exports.Fieldset = void 0;
7
+ var _react = require("react");
8
+ var _css = require("@ndla/styled-system/css");
9
+ var _jsx2 = require("@ndla/styled-system/jsx");
10
+ var _FormControl = require("./FormControl");
11
+ var _jsxRuntime = require("react/jsx-runtime");
12
+ /**
13
+ * Copyright (c) 2024-present, NDLA.
14
+ *
15
+ * This source code is licensed under the GPLv3 license found in the
16
+ * LICENSE file in the root directory of this source tree.
17
+ *
18
+ */
19
+
20
+ const StyledLegend = (0, _jsx2.styled)("legend", {
21
+ base: {
22
+ float: "none",
23
+ width: "inherit",
24
+ _disabled: {
25
+ color: "text.subtle"
26
+ }
27
+ }
28
+ });
29
+ const Legend = exports.Legend = /*#__PURE__*/(0, _react.forwardRef)((_ref, ref) => {
30
+ let {
31
+ textStyle = "label.medium",
32
+ fontWeight = "bold",
33
+ className,
34
+ srOnly,
35
+ ...rest
36
+ } = _ref;
37
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledLegend, {
38
+ className: (0, _css.cx)((0, _css.css)({
39
+ textStyle,
40
+ fontWeight,
41
+ srOnly
42
+ }), className),
43
+ ...rest,
44
+ ref: ref
45
+ });
46
+ });
47
+ const FormLegend = exports.FormLegend = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
48
+ var _control$getLabelProp;
49
+ const control = (0, _FormControl.useFormControlContext)();
50
+ // Legend does not use htmlFor (for), so we remove it.
51
+ const {
52
+ id: _,
53
+ htmlFor: __,
54
+ ...fieldProps
55
+ } = (_control$getLabelProp = control === null || control === void 0 ? void 0 : control.getLabelProps(props, ref)) !== null && _control$getLabelProp !== void 0 ? _control$getLabelProp : {
56
+ ref,
57
+ ...props
58
+ };
59
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(Legend, {
60
+ ...props,
61
+ ...fieldProps,
62
+ ref: ref
63
+ });
64
+ });
65
+ const StyledLabel = (0, _jsx2.styled)("label", {
66
+ base: {
67
+ display: "inline-block",
68
+ _disabled: {
69
+ color: "text.subtle"
70
+ }
71
+ }
72
+ });
73
+ const Label = exports.Label = /*#__PURE__*/(0, _react.forwardRef)((_ref2, ref) => {
74
+ let {
75
+ textStyle = "label.medium",
76
+ fontWeight = "bold",
77
+ className,
78
+ srOnly,
79
+ ...rest
80
+ } = _ref2;
81
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(StyledLabel, {
82
+ className: (0, _css.cx)((0, _css.css)({
83
+ textStyle,
84
+ fontWeight,
85
+ srOnly
86
+ }), className),
87
+ ...rest,
88
+ ref: ref
89
+ });
90
+ });
91
+ const FormLabel = exports.FormLabel = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
92
+ var _control$getLabelProp2;
93
+ const control = (0, _FormControl.useFormControlContext)();
94
+ const fieldProps = (_control$getLabelProp2 = control === null || control === void 0 ? void 0 : control.getLabelProps(props, ref)) !== null && _control$getLabelProp2 !== void 0 ? _control$getLabelProp2 : {
95
+ ref,
96
+ ...props
97
+ };
98
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(Label, {
99
+ ...props,
100
+ ...fieldProps,
101
+ ref: ref
102
+ });
103
+ });
104
+ const Fieldset = exports.Fieldset = (0, _jsx2.styled)("fieldset", {
105
+ base: {
106
+ border: "none",
107
+ padding: "0",
108
+ margin: "0"
109
+ }
110
+ });