From 04ab736ffd7a7b0f98676c61eb2c0a946b040392 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Tue, 4 Oct 2022 10:50:01 -0400 Subject: [PATCH] fix: deprecates nanoid --- package.json | 1 - src/SliderProvider/index.tsx | 6 +++--- src/makeID.ts | 15 +++++++++++++++ yarn.lock | 5 ----- 4 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 src/makeID.ts diff --git a/package.json b/package.json index 6577fae..be5fd24 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0" }, "dependencies": { - "nanoid": "^4.0.0", "smoothscroll-polyfill": "^0.4.4" }, "devDependencies": { diff --git a/src/SliderProvider/index.tsx b/src/SliderProvider/index.tsx index a8922b7..4859999 100644 --- a/src/SliderProvider/index.tsx +++ b/src/SliderProvider/index.tsx @@ -12,7 +12,7 @@ import { useBreakpoints } from './useBreakpoints'; import { useMarquee } from './useMarquee'; import { useAutoplay } from './useAutoplay'; import { useScrollToIndex } from './useScrollToIndex'; -import { nanoid } from 'nanoid'; +import { makeID } from '../makeID'; export type ChildFunction = (context: ISliderContext) => React.ReactNode; // eslint-disable-line no-unused-vars @@ -53,10 +53,10 @@ const SliderProvider: React.FC = (props) => { } = props; // NOTE: the 'aria-controls' attribute relies on this matching IDs - const [id, setID] = useState(() => idFromProps || nanoid(6)); + const [id, setID] = useState(() => idFromProps || makeID(5)); useEffect(() => { - setID(idFromProps || nanoid(6)); + setID(idFromProps || makeID(5)); }, [idFromProps]) const settings = useBreakpoints(props); diff --git a/src/makeID.ts b/src/makeID.ts new file mode 100644 index 0000000..a039949 --- /dev/null +++ b/src/makeID.ts @@ -0,0 +1,15 @@ +export const makeID = (length: number) => { + let result = ''; + + const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + const charactersLength = characters.length; + + characters.split('').forEach((char, index) => { + if (index < length) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + }); + + return result; + +} diff --git a/yarn.lock b/yarn.lock index 7e65a3b..82ebf6d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3862,11 +3862,6 @@ multicast-dns@^7.2.5: dns-packet "^5.2.2" thunky "^1.0.2" -nanoid@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-4.0.0.tgz#6e144dee117609232c3f415c34b0e550e64999a5" - integrity sha512-IgBP8piMxe/gf73RTQx7hmnhwz0aaEXYakvqZyE302IXW3HyVNhdNGC+O2MwMAVhLEnvXlvKtGbtJf6wvHihCg== - natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"