2021-04-27 21:01:44 +02:00
|
|
|
<!--
|
2022-02-03 05:52:11 +01:00
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-04-27 21:01:44 +02:00
|
|
|
-->
|
2022-02-03 05:52:11 +01:00
|
|
|
<script context="module" lang="ts">
|
|
|
|
import type { Writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { get, writable } from "svelte/store";
|
|
|
|
|
|
|
|
import contextProperty from "../sveltelib/context-property";
|
2022-02-03 05:52:11 +01:00
|
|
|
import type {
|
|
|
|
GetSlotHostProps,
|
2022-02-04 09:36:34 +01:00
|
|
|
SlotHostProps,
|
2022-02-03 05:52:11 +01:00
|
|
|
} from "../sveltelib/dynamic-slotting";
|
|
|
|
|
|
|
|
enum ButtonPosition {
|
|
|
|
Standalone,
|
|
|
|
InlineStart,
|
|
|
|
Center,
|
|
|
|
InlineEnd,
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ButtonSlotHostProps extends SlotHostProps {
|
|
|
|
position: Writable<ButtonPosition>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const key = Symbol("buttonGroup");
|
|
|
|
const [context, setSlotHostContext] =
|
|
|
|
contextProperty<GetSlotHostProps<ButtonSlotHostProps>>(key);
|
|
|
|
|
|
|
|
export { setSlotHostContext };
|
|
|
|
|
|
|
|
export function createProps(): ButtonSlotHostProps {
|
|
|
|
return {
|
|
|
|
detach: writable(false),
|
|
|
|
position: writable(ButtonPosition.Standalone),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function nonDetached(props: ButtonSlotHostProps): boolean {
|
|
|
|
return !get(props.detach);
|
|
|
|
}
|
2021-05-05 01:22:51 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
export function updatePropsList(
|
|
|
|
propsList: ButtonSlotHostProps[],
|
|
|
|
): ButtonSlotHostProps[] {
|
|
|
|
const list = Array.from(propsList.filter(nonDetached).entries());
|
2021-05-06 01:22:55 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
for (const [index, props] of list) {
|
|
|
|
const position = props.position;
|
2021-04-27 21:01:44 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
if (list.length === 1) {
|
|
|
|
position.set(ButtonPosition.Standalone);
|
|
|
|
} else if (index === 0) {
|
|
|
|
position.set(ButtonPosition.InlineStart);
|
|
|
|
} else if (index === list.length - 1) {
|
|
|
|
position.set(ButtonPosition.InlineEnd);
|
|
|
|
} else {
|
|
|
|
position.set(ButtonPosition.Center);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return propsList;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-05-06 16:10:26 +02:00
|
|
|
export let id: string | undefined = undefined;
|
2022-02-03 05:52:11 +01:00
|
|
|
export let hostProps: ButtonSlotHostProps | undefined = undefined;
|
2021-05-06 01:22:55 +02:00
|
|
|
|
|
|
|
let style: string;
|
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
if (!context.available()) {
|
|
|
|
console.log("ButtonGroupItem: should always have a slotHostContext");
|
|
|
|
}
|
2021-05-06 01:22:55 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
const { detach, position } = hostProps ?? context.get().getProps();
|
|
|
|
const radius = "5px";
|
2021-09-02 21:01:29 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
function updateButtonStyle(position: ButtonPosition) {
|
|
|
|
switch (position) {
|
2021-05-06 01:22:55 +02:00
|
|
|
case ButtonPosition.Standalone:
|
|
|
|
style = `--border-left-radius: ${radius}; --border-right-radius: ${radius}; `;
|
|
|
|
break;
|
2021-09-02 21:01:29 +02:00
|
|
|
case ButtonPosition.InlineStart:
|
2022-02-03 05:52:11 +01:00
|
|
|
style = `--border-left-radius: ${radius}; --border-right-radius: 0; `;
|
2021-05-06 01:22:55 +02:00
|
|
|
break;
|
|
|
|
case ButtonPosition.Center:
|
|
|
|
style = "--border-left-radius: 0; --border-right-radius: 0; ";
|
|
|
|
break;
|
2021-09-02 21:01:29 +02:00
|
|
|
case ButtonPosition.InlineEnd:
|
2022-02-03 05:52:11 +01:00
|
|
|
style = `--border-left-radius: 0; --border-right-radius: ${radius}; `;
|
2021-05-06 01:22:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 15:12:02 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
$: updateButtonStyle($position);
|
2021-04-27 21:01:44 +02:00
|
|
|
</script>
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
<!-- div is necessary to preserve item position -->
|
2022-02-03 05:52:11 +01:00
|
|
|
<div class="button-group-item" {id} {style}>
|
|
|
|
{#if !$detach}
|
2021-05-06 01:22:55 +02:00
|
|
|
<slot />
|
2022-02-03 05:52:11 +01:00
|
|
|
{/if}
|
2021-10-18 14:01:15 +02:00
|
|
|
</div>
|
2021-10-28 11:37:52 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.button-group-item {
|
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
</style>
|