Skip to content

Commit

Permalink
Merge pull request #88 from vector-im/johannes/pony-fill-use-id
Browse files Browse the repository at this point in the history
  • Loading branch information
Johennes authored Sep 26, 2023
2 parents 4cb7ec4 + 7898f19 commit 18afe4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/ActionControl/ActionControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ limitations under the License.
*/

import classnames from "classnames";
import React, { PropsWithChildren, useId, forwardRef } from "react";
import React, { PropsWithChildren, forwardRef } from "react";
import styles from "./ActionControl.module.css";

import { Control, Field, Root } from "../Form";
import useId from "../../utils/useId";

type ActionControlProps = {
/**
Expand Down
32 changes: 32 additions & 0 deletions src/utils/useId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import * as React from "react";

const react18UseId = (React as { useId?: () => string }).useId;

const getUniqueId = (() => {
let index = 1;
return () => `:r${index++}:`;
})();

const useIdPonyFill = (): string => {
return React.useMemo(getUniqueId, []);
};

const useId = typeof react18UseId === "function" ? react18UseId : useIdPonyFill;

export default useId;

0 comments on commit 18afe4b

Please sign in to comment.