@codecademy/gamut-styles 17.13.2-alpha.d5be1a.0 → 17.13.2-alpha.d7c4c0.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.
- package/dist/utilities/elementDir.d.ts +22 -0
- package/dist/utilities/{directionIsRtl.js → elementDir.js} +20 -11
- package/dist/utilities/index.d.ts +1 -1
- package/dist/utilities/index.js +1 -1
- package/dist/utilities/useLogicalProperties.d.ts +2 -3
- package/dist/utilities/useLogicalProperties.js +2 -3
- package/package.json +2 -2
- package/dist/utilities/directionIsRtl.d.ts +0 -14
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RefObject } from 'react';
|
|
2
|
+
/** Resolved HTML `dir` keyword: effective writing direction after `dir`, then CSS `direction`, then document root. */
|
|
3
|
+
export type DirValue = 'rtl' | 'ltr';
|
|
4
|
+
/**
|
|
5
|
+
* Resolves the effective `dir` for an element (`rtl` or `ltr`), including JSDOM where
|
|
6
|
+
* `getComputedStyle(el).direction` is often empty while `dir` is set on the root.
|
|
7
|
+
*/
|
|
8
|
+
export declare function elementDir(el: Element): DirValue;
|
|
9
|
+
/**
|
|
10
|
+
* Ref whose `current` may be any DOM {@link Element} subclass (`HTMLElement`, `SVGElement`,
|
|
11
|
+
* `HTMLButtonElement`, etc.). For structural/minimal types (e.g. tests), intersect with
|
|
12
|
+
* `Element` so `current` is still typed as an `Element` (e.g. `Pick<HTMLAnchorElement, 'id'> & Element`).
|
|
13
|
+
*/
|
|
14
|
+
export type ElementDirRef<T extends Element = Element> = RefObject<T | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Returns the effective `dir` for the resolved element (`rtl` or `ltr`), and updates when `dir`
|
|
17
|
+
* changes on the document subtree or after layout (so `ref.current` is current).
|
|
18
|
+
* Resolution uses {@link elementDir}.
|
|
19
|
+
*
|
|
20
|
+
* @param elementRef - Optional ref; when missing or `current` is not an `Element`, uses `document.documentElement`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function useElementDir<T extends Element = Element>(elementRef?: ElementDirRef<T>): DirValue;
|
|
@@ -1,33 +1,42 @@
|
|
|
1
1
|
import { useEffect, useLayoutEffect, useReducer } from 'react';
|
|
2
2
|
|
|
3
|
+
/** Resolved HTML `dir` keyword: effective writing direction after `dir`, then CSS `direction`, then document root. */
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
|
-
* Resolves
|
|
6
|
+
* Resolves the effective `dir` for an element (`rtl` or `ltr`), including JSDOM where
|
|
5
7
|
* `getComputedStyle(el).direction` is often empty while `dir` is set on the root.
|
|
6
8
|
*/
|
|
7
|
-
export function
|
|
9
|
+
export function elementDir(el) {
|
|
8
10
|
const ownDir = el.getAttribute('dir');
|
|
9
|
-
if (ownDir === 'rtl') return
|
|
10
|
-
if (ownDir === 'ltr') return
|
|
11
|
+
if (ownDir === 'rtl') return 'rtl';
|
|
12
|
+
if (ownDir === 'ltr') return 'ltr';
|
|
11
13
|
const {
|
|
12
14
|
direction
|
|
13
15
|
} = getComputedStyle(el);
|
|
14
16
|
if (direction === 'rtl' || direction === 'ltr') {
|
|
15
|
-
return direction
|
|
17
|
+
return direction;
|
|
16
18
|
}
|
|
17
|
-
return document.documentElement.getAttribute('dir') === 'rtl';
|
|
19
|
+
return document.documentElement.getAttribute('dir') === 'rtl' ? 'rtl' : 'ltr';
|
|
18
20
|
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Ref whose `current` may be any DOM {@link Element} subclass (`HTMLElement`, `SVGElement`,
|
|
24
|
+
* `HTMLButtonElement`, etc.). For structural/minimal types (e.g. tests), intersect with
|
|
25
|
+
* `Element` so `current` is still typed as an `Element` (e.g. `Pick<HTMLAnchorElement, 'id'> & Element`).
|
|
26
|
+
*/
|
|
27
|
+
|
|
19
28
|
function resolveElement(elementRef) {
|
|
20
29
|
return elementRef?.current instanceof Element ? elementRef.current : document.documentElement;
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
/**
|
|
24
|
-
* Returns
|
|
33
|
+
* Returns the effective `dir` for the resolved element (`rtl` or `ltr`), and updates when `dir`
|
|
25
34
|
* changes on the document subtree or after layout (so `ref.current` is current).
|
|
26
|
-
* Resolution uses {@link
|
|
35
|
+
* Resolution uses {@link elementDir}.
|
|
27
36
|
*
|
|
28
37
|
* @param elementRef - Optional ref; when missing or `current` is not an `Element`, uses `document.documentElement`.
|
|
29
38
|
*/
|
|
30
|
-
export function
|
|
39
|
+
export function useElementDir(elementRef) {
|
|
31
40
|
const [, bump] = useReducer(n => n + 1, 0);
|
|
32
41
|
useLayoutEffect(() => {
|
|
33
42
|
bump();
|
|
@@ -44,7 +53,7 @@ export function useDirectionIsRtl(elementRef) {
|
|
|
44
53
|
return () => observer.disconnect();
|
|
45
54
|
}, []);
|
|
46
55
|
if (typeof document === 'undefined') {
|
|
47
|
-
return
|
|
56
|
+
return 'ltr';
|
|
48
57
|
}
|
|
49
|
-
return
|
|
58
|
+
return elementDir(resolveElement(elementRef));
|
|
50
59
|
}
|
package/dist/utilities/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Whether Gamut system props emit logical CSS properties (`marginInlineStart`, etc.)
|
|
3
|
-
* vs physical (`marginLeft`, etc.).
|
|
4
|
-
* omits the field: `theme.useLogicalProperties ?? true`.
|
|
3
|
+
* vs physical (`marginLeft`, etc.).
|
|
5
4
|
*
|
|
6
5
|
* `GamutProvider` always merges an explicit boolean (default `false`).
|
|
7
6
|
*/
|
|
8
|
-
export declare function useLogicalProperties(): boolean;
|
|
7
|
+
export declare function useLogicalProperties(): boolean | undefined;
|
|
@@ -2,12 +2,11 @@ import { useTheme } from '@emotion/react';
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Whether Gamut system props emit logical CSS properties (`marginInlineStart`, etc.)
|
|
5
|
-
* vs physical (`marginLeft`, etc.).
|
|
6
|
-
* omits the field: `theme.useLogicalProperties ?? true`.
|
|
5
|
+
* vs physical (`marginLeft`, etc.).
|
|
7
6
|
*
|
|
8
7
|
* `GamutProvider` always merges an explicit boolean (default `false`).
|
|
9
8
|
*/
|
|
10
9
|
export function useLogicalProperties() {
|
|
11
10
|
const theme = useTheme();
|
|
12
|
-
return theme
|
|
11
|
+
return theme?.useLogicalProperties;
|
|
13
12
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codecademy/gamut-styles",
|
|
3
3
|
"description": "Styleguide & Component library for codecademy.com",
|
|
4
|
-
"version": "17.13.2-alpha.
|
|
4
|
+
"version": "17.13.2-alpha.d7c4c0.0",
|
|
5
5
|
"author": "Jake Hiller <jake@codecademy.com>",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@codecademy/variance": "0.26.2-alpha.
|
|
7
|
+
"@codecademy/variance": "0.26.2-alpha.d7c4c0.0",
|
|
8
8
|
"@emotion/is-prop-valid": "^1.1.0",
|
|
9
9
|
"framer-motion": "^11.18.0",
|
|
10
10
|
"get-nonce": "^1.0.0",
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { RefObject } from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* Resolves whether layout direction is RTL for an element, including JSDOM where
|
|
4
|
-
* `getComputedStyle(el).direction` is often empty while `dir` is set on the root.
|
|
5
|
-
*/
|
|
6
|
-
export declare function directionIsRtl(el: Element): boolean;
|
|
7
|
-
/**
|
|
8
|
-
* Returns whether the resolved element’s direction is RTL, and updates when `dir`
|
|
9
|
-
* changes on the document subtree or after layout (so `ref.current` is current).
|
|
10
|
-
* Resolution uses {@link directionIsRtl}.
|
|
11
|
-
*
|
|
12
|
-
* @param elementRef - Optional ref; when missing or `current` is not an `Element`, uses `document.documentElement`.
|
|
13
|
-
*/
|
|
14
|
-
export declare function useDirectionIsRtl(elementRef?: RefObject<Element | null>): boolean;
|