react-smart-copy 1.15.0 → 1.16.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/README.md CHANGED
@@ -33,6 +33,7 @@ ID PLT-29018 [Copy]
33
33
  - **`react-smart-copy/code`**: copy code blocks the way developers expect (a terminal session copies as its commands, gutters and prompts stay behind, colours survive in Docs and Slack), paste code cleaned and indented, and catch Trojan Source, invisible identifiers and lookalike letters
34
34
  - **`react-smart-copy/image`**: get pasted photos and screenshots ready to upload. It removes GPS location, camera data and hidden thumbnails without touching a pixel, and fits size and byte limits, re-encoding only when it has to
35
35
  - **`react-smart-copy/link`**: "Copy link" buttons that paste as a titled link in Slack, Docs and Notion and as the URL in editors, links that scroll to a highlighted passage, tracking parameters removed, and pasting a URL over selected text to make a link
36
+ - **`react-smart-copy/color`**: copy colours in any CSS format (hex to `oklch()` and Display P3) with a swatch for Docs and Slack, paste any format into a colour field, and check WCAG contrast. Wide-gamut colours are mapped the way CSS specifies, never clipped to a wrong hue
36
37
  - **`react-smart-copy/share`**: one "Share" button that opens the share sheet on phones and copies a titled link on desktop, with honest states: a closed share sheet is never an error
37
38
  - **`react-smart-copy/capture`**: copy an `<svg>` or any piece of your UI as a PNG
38
39
  - Every failure is a typed, classified error. Nothing silently no-ops
@@ -57,6 +58,7 @@ This package only promises what browsers actually support.
57
58
  | Code (blocks, sessions, diffs) | ✅ | `react-smart-copy/code`: plain text plus monospaced, highlighted HTML; commands without prompts or output; Trojan Source checks on paste |
58
59
  | Pasted images, made safe | ✅ paste | `react-smart-copy/image`: removes location and camera metadata losslessly; resizes and compresses under a byte budget |
59
60
  | Links (titled, to a passage) | ✅ | `react-smart-copy/link`: a titled `<a>` plus the URL; `text/uri-list` when dragged; text fragment links; never `javascript:` |
61
+ | Colours (every CSS format) | ✅ | `react-smart-copy/color`: the value as text, plus a swatch for rich-text apps; any CSS colour read back on paste |
60
62
  | Share sheet (Web Share API) | ✅ with copy fallback | `react-smart-copy/share`: the share sheet on phones and tablets, a titled link copied elsewhere; files where the share sheet takes them |
61
63
  | SVG (icons, charts, logos) | ✅ as PNG | `svgToPngBlob()` from `react-smart-copy/capture` rasterises natively, no dependency. Raw SVG is not a clipboard image format |
62
64
  | A section of your UI as an image | ✅ bring a rasteriser | Browsers have no "screenshot this element" API. `captureElement()` wraps one you choose (e.g. `modern-screenshot`) with limits and checks |
@@ -934,6 +936,70 @@ usePaste({ accept: ["text", "html", "text/uri-list"], onPaste: (result) => {
934
936
  | `maxTitleLength` | 300 | |
935
937
  | `maxWords` / `contextWords` | 10 / 4 | text fragments |
936
938
 
939
+ ## Copy and paste colours
940
+
941
+ `react-smart-copy/color` is for design-system docs, colour pickers, theme editors and
942
+ design tools. It reads and writes every CSS Color 4 notation, without dependencies.
943
+ About 6.5 kB in total; `parseColor` with `formatColor` is about 5.3 kB.
944
+
945
+ ```tsx
946
+ import { useCopy } from "react-smart-copy";
947
+ import { colorPayload, pasteAsColor } from "react-smart-copy/color";
948
+
949
+ const { copy } = useCopy();
950
+ <button onClick={() => copy(() => colorPayload(token.value, { format: "oklch" }))}>Copy</button>
951
+ // Text: "oklch(62.31% 0.18801 259.81)". Docs, Notion, Slack and mail also get a swatch.
952
+
953
+ <input value={hex} onChange={…} onPaste={(event) => pasteAsColor(event, { format: "hex" })} />
954
+ // Pasting "rgb(59 130 246)" or "background: #3b82f6;" inserts "#3b82f6".
955
+ ```
956
+
957
+ **Every format, both ways.** `parseColor` reads hex (3, 4, 6 and 8 digits), the 148 named
958
+ colours and `transparent`, `rgb()` and `hsl()` (both syntaxes), `hwb()`, `lab()`, `lch()`,
959
+ `oklab()`, `oklch()`, and `color()` in `srgb`, `srgb-linear`, `display-p3`, `xyz`, `xyz-d65`
960
+ and `xyz-d50`, with every angle unit, percentages and `none`. It returns `null` for
961
+ anything else and never throws. `formatColor(color, format)` writes `hex`, `rgb`, `hsl`,
962
+ `hwb`, `oklch`, `oklab`, `lab`, `lch` or `p3`. Its default precision brings every 8-bit
963
+ colour back exactly; this was checked on 836,056 colours.
964
+
965
+ **Wide gamut done right.** A Display P3 or OKLCH colour outside sRGB stays exact until a
966
+ format needs it in sRGB. Then it is gamut-mapped as CSS Color 4 specifies: lightness and
967
+ hue are kept and chroma is reduced, so a vivid P3 red becomes the closest sRGB red, not an
968
+ orange. `gamut: "clip"` clips each channel instead, as most browsers draw. `inGamut` and
969
+ `toGamut` are exported.
970
+
971
+ **Pasting.** `readColor(text)` reads the one colour in pasted text. That includes a
972
+ declaration copied from a stylesheet or developer tools: `color: #3b82f6;`,
973
+ `--brand: oklch(…) !important;`. `bareHex: true` also reads `3b82f6`; it is off by default,
974
+ because words like "decade" are hex. `pasteAsColor` is a paste handler for colour fields
975
+ that keeps the browser's undo.
976
+
977
+ **Contrast.** `contrastRatio(text, background)` is the WCAG 2 ratio. Translucent colours
978
+ are drawn the way a browser draws them, and the ratio is never rounded, so 4.499 fails.
979
+ `contrastLevel(ratio, { large })` gives `AAA`, `AA` or `fail`.
980
+
981
+ ```ts
982
+ import { contrastLevel, contrastRatio, formatColor, parseColor } from "react-smart-copy/color";
983
+
984
+ formatColor("color(display-p3 1 0 0)", "hex"); // "#ff0b0c": mapped, not clipped
985
+ formatColor("#3b82f6", "hsl", { legacy: true }); // "hsl(217.22, 91.22%, 59.8%)"
986
+ contrastLevel(contrastRatio("#767676", "#fff")); // "AA" (4.54)
987
+ parseColor("hsl(1turn 100% 50% / 50%)"); // { r: 1, g: 0, b: 0, alpha: 0.5 }
988
+ ```
989
+
990
+ | Option (`formatColor`, `colorPayload`) | Default | |
991
+ | --- | --- | --- |
992
+ | `format` | `"hex"` | `colorPayload` only |
993
+ | `gamut` | `"map"` | `"clip"` |
994
+ | `legacy` | `false` | comma syntax for `rgb` and `hsl` |
995
+ | `uppercase` | `false` | hex digits |
996
+ | `precision` | per format | decimal places |
997
+ | `swatch` | `true` | `colorPayload` only |
998
+
999
+ Accuracy is property-tested against [culori](https://culorijs.org) for every notation and
1000
+ format. In Chromium, Firefox and WebKit, colours are also checked against the browser's
1001
+ own CSS parser.
1002
+
937
1003
  ## Share, or copy the link
938
1004
 
939
1005
  `react-smart-copy/share` is one button that does the right thing on every device: the
@@ -1447,7 +1513,8 @@ embedded webviews. Where a capability is missing you get a typed error, never a
1447
1513
  - ~~**1.12** `react-smart-copy/code` — copy code blocks and sessions cleanly, paste code safely, detect Trojan Source and homoglyphs~~ shipped
1448
1514
  - ~~**1.13** `react-smart-copy/image` — pasted images without location or camera metadata, fitted to size and byte limits~~ shipped
1449
1515
  - ~~**1.14** `react-smart-copy/link` — links that paste well everywhere, links to a passage, tracking parameters removed, paste a URL over text to make a link~~ shipped
1450
- - **1.15** `react-smart-copy/share` — the share sheet on phones, a copied titled link everywhere else, with honest outcomes
1516
+ - ~~**1.15** `react-smart-copy/share` — the share sheet on phones, a copied titled link everywhere else, with honest outcomes~~ shipped
1517
+ - **1.16** `react-smart-copy/color` — copy and paste colours in every CSS format, CSS gamut mapping, WCAG contrast
1451
1518
 
1452
1519
  Full history: [CHANGELOG.md](./CHANGELOG.md) or the [Releases page](https://suprabhat02.github.io/react-smart-copy/#releases).
1453
1520