@kobalte/core
Version:
Unstyled components and primitives for building accessible web apps and design systems with SolidJS.
90 lines (84 loc) • 2.45 kB
JavaScript
import { createTagName } from './ET5T45DO.js';
import { Polymorphic } from './6Y7B2NEO.js';
import { __export } from './5ZKAE4VZ.js';
import { createComponent, mergeProps } from 'solid-js/web';
import { mergeDefaultProps, mergeRefs } from '@kobalte/utils';
import { splitProps, createMemo } from 'solid-js';
// src/button/index.tsx
var button_exports = {};
__export(button_exports, {
Button: () => Button,
Root: () => ButtonRoot
});
// src/button/is-button.ts
var BUTTON_INPUT_TYPES = [
"button",
"color",
"file",
"image",
"reset",
"submit"
];
function isButton(element) {
const tagName = element.tagName.toLowerCase();
if (tagName === "button") {
return true;
}
if (tagName === "input" && element.type) {
return BUTTON_INPUT_TYPES.indexOf(element.type) !== -1;
}
return false;
}
// src/button/button-root.tsx
function ButtonRoot(props) {
let ref;
const mergedProps = mergeDefaultProps({
type: "button"
}, props);
const [local, others] = splitProps(mergedProps, ["ref", "type", "disabled"]);
const tagName = createTagName(() => ref, () => "button");
const isNativeButton = createMemo(() => {
const elementTagName = tagName();
if (elementTagName == null) {
return false;
}
return isButton({
tagName: elementTagName,
type: local.type
});
});
const isNativeInput = createMemo(() => {
return tagName() === "input";
});
const isNativeLink = createMemo(() => {
return tagName() === "a" && ref?.getAttribute("href") != null;
});
return createComponent(Polymorphic, mergeProps({
as: "button",
ref(r$) {
const _ref$ = mergeRefs((el) => ref = el, local.ref);
typeof _ref$ === "function" && _ref$(r$);
},
get type() {
return isNativeButton() || isNativeInput() ? local.type : void 0;
},
get role() {
return !isNativeButton() && !isNativeLink() ? "button" : void 0;
},
get tabIndex() {
return !isNativeButton() && !isNativeLink() && !local.disabled ? 0 : void 0;
},
get disabled() {
return isNativeButton() || isNativeInput() ? local.disabled : void 0;
},
get ["aria-disabled"]() {
return !isNativeButton() && !isNativeInput() && local.disabled ? true : void 0;
},
get ["data-disabled"]() {
return local.disabled ? "" : void 0;
}
}, others));
}
// src/button/index.tsx
var Button = ButtonRoot;
export { Button, ButtonRoot, button_exports };