2021-04-27 21:01:44 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="typescript">
|
2021-07-09 16:50:12 +02:00
|
|
|
import WithTheming from "./WithTheming.svelte";
|
|
|
|
import Detachable from "./Detachable.svelte";
|
2021-05-05 01:22:51 +02:00
|
|
|
|
2021-05-06 01:22:55 +02:00
|
|
|
import type { ButtonRegistration } from "./buttons";
|
|
|
|
import { ButtonPosition } from "./buttons";
|
2021-05-06 18:51:44 +02:00
|
|
|
import type { Register } from "./registration";
|
2021-05-06 01:22:55 +02:00
|
|
|
|
|
|
|
import { getContext, hasContext } from "svelte";
|
2021-06-30 19:55:56 +02:00
|
|
|
import { buttonGroupKey } from "./context-keys";
|
2021-04-27 21:01:44 +02:00
|
|
|
|
2021-05-06 16:10:26 +02:00
|
|
|
export let id: string | undefined = undefined;
|
2021-05-06 01:22:55 +02:00
|
|
|
export let registration: ButtonRegistration | undefined = undefined;
|
|
|
|
|
2021-05-07 14:31:08 +02:00
|
|
|
let detached: boolean;
|
2021-05-06 01:22:55 +02:00
|
|
|
let position_: ButtonPosition;
|
|
|
|
let style: string;
|
|
|
|
|
2021-08-31 23:10:16 +02:00
|
|
|
const radius = "5px";
|
2021-05-06 01:22:55 +02:00
|
|
|
|
2021-09-02 21:01:29 +02:00
|
|
|
const leftStyle = `--border-left-radius: ${radius}; --border-right-radius: 0; `;
|
|
|
|
const rightStyle = `--border-left-radius: 0; --border-right-radius: ${radius}; `;
|
|
|
|
|
2021-05-06 01:22:55 +02:00
|
|
|
$: {
|
|
|
|
switch (position_) {
|
|
|
|
case ButtonPosition.Standalone:
|
|
|
|
style = `--border-left-radius: ${radius}; --border-right-radius: ${radius}; `;
|
|
|
|
break;
|
2021-09-02 21:01:29 +02:00
|
|
|
case ButtonPosition.InlineStart:
|
|
|
|
style = leftStyle;
|
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:
|
|
|
|
style = rightStyle;
|
2021-05-06 01:22:55 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 15:12:02 +02:00
|
|
|
|
2021-05-06 01:22:55 +02:00
|
|
|
if (registration) {
|
|
|
|
const { detach, position } = registration;
|
2021-05-07 14:31:08 +02:00
|
|
|
detach.subscribe((value: boolean) => (detached = value));
|
2021-05-06 01:22:55 +02:00
|
|
|
position.subscribe((value: ButtonPosition) => (position_ = value));
|
|
|
|
} else if (hasContext(buttonGroupKey)) {
|
2021-05-26 01:21:33 +02:00
|
|
|
const registerComponent =
|
|
|
|
getContext<Register<ButtonRegistration>>(buttonGroupKey);
|
2021-05-06 18:51:44 +02:00
|
|
|
const { detach, position } = registerComponent();
|
2021-05-07 14:31:08 +02:00
|
|
|
detach.subscribe((value: boolean) => (detached = value));
|
2021-05-06 01:22:55 +02:00
|
|
|
position.subscribe((value: ButtonPosition) => (position_ = value));
|
|
|
|
} else {
|
2021-05-07 14:31:08 +02:00
|
|
|
detached = false;
|
2021-05-06 01:22:55 +02:00
|
|
|
position_ = ButtonPosition.Standalone;
|
|
|
|
}
|
2021-04-27 21:01:44 +02:00
|
|
|
</script>
|
|
|
|
|
2021-05-06 01:22:55 +02:00
|
|
|
<!-- div in WithTheming is necessary to preserve item position -->
|
2021-05-06 16:10:26 +02:00
|
|
|
<WithTheming {id} {style}>
|
2021-05-07 14:31:08 +02:00
|
|
|
<Detachable {detached}>
|
2021-05-06 01:22:55 +02:00
|
|
|
<slot />
|
|
|
|
</Detachable>
|
|
|
|
</WithTheming>
|