Skip to content

Commit

Permalink
Add option setters
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Mar 29, 2024
1 parent 3cfa213 commit 492123a
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 40 deletions.
32 changes: 32 additions & 0 deletions packages/core/src/detect_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,38 @@ pub struct DetectOverflowOptions<Element> {
pub padding: Option<Padding>,
}

impl<Element> DetectOverflowOptions<Element> {
/// Set `boundary` option.
pub fn boundary(mut self, value: Boundary<Element>) -> Self {
self.boundary = Some(value);
self
}

/// Set `root_boundary` option.
pub fn root_boundary(mut self, value: RootBoundary) -> Self {
self.root_boundary = Some(value);
self
}

/// Set `element_context` option.
pub fn element_context(mut self, value: ElementContext) -> Self {
self.element_context = Some(value);
self
}

/// Set `alt_boundary` option.
pub fn alt_boundary(mut self, value: bool) -> Self {
self.alt_boundary = Some(value);
self
}

/// Set `padding` option.
pub fn padding(mut self, value: Padding) -> Self {
self.padding = Some(value);
self
}
}

impl<Element> Default for DetectOverflowOptions<Element> {
fn default() -> Self {
Self {
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/middleware/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ pub struct ArrowOptions<Element: Clone> {
pub padding: Option<Padding>,
}

impl<Element: Clone> ArrowOptions<Element> {
pub fn new(element: Element) -> Self {
ArrowOptions {
element,
padding: None,
}
}

/// Set `element` option.
pub fn element(mut self, value: Element) -> Self {
self.element = value;
self
}

/// Set `padding` option.
pub fn padding(mut self, value: Padding) -> Self {
self.padding = Some(value);
self
}
}

/// Data stored by [`Arrow`] middleware.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ArrowData {
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/middleware/auto_placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ pub struct AutoPlacementOptions<Element: Clone> {
pub allowed_placements: Option<Vec<Placement>>,
}

impl<Element: Clone> AutoPlacementOptions<Element> {
/// Set `detect_overflow` option.
pub fn detect_overflow(mut self, value: DetectOverflowOptions<Element>) -> Self {
self.detect_overflow = Some(value);
self
}

/// Set `cross_axis` option.
pub fn cross_axis(mut self, value: bool) -> Self {
self.cross_axis = Some(value);
self
}

/// Set `alignment` option.
pub fn alignment(mut self, value: Alignment) -> Self {
self.alignment = Some(value);
self
}

/// Set `auto_alignment` option.
pub fn auto_alignment(mut self, value: bool) -> Self {
self.auto_alignment = Some(value);
self
}

/// Set `alignment` option.
pub fn allowed_placements(mut self, value: Vec<Placement>) -> Self {
self.allowed_placements = Some(value);
self
}
}

impl<Element: Clone> Default for AutoPlacementOptions<Element> {
fn default() -> Self {
Self {
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/middleware/flip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,50 @@ pub struct FlipOptions<Element: Clone> {
pub flip_alignment: Option<bool>,
}

impl<Element: Clone> FlipOptions<Element> {
/// Set `detect_overflow` option.
pub fn detect_overflow(mut self, value: DetectOverflowOptions<Element>) -> Self {
self.detect_overflow = Some(value);
self
}

/// Set `main_axis` option.
pub fn main_axis(mut self, value: bool) -> Self {
self.main_axis = Some(value);
self
}

/// Set `cross_axis` option.
pub fn cross_axis(mut self, value: bool) -> Self {
self.cross_axis = Some(value);
self
}

/// Set `fallback_placements` option.
pub fn fallback_placements(mut self, value: Vec<Placement>) -> Self {
self.fallback_placements = Some(value);
self
}

/// Set `cross_axis` option.
pub fn fallback_strategy(mut self, value: FallbackStrategy) -> Self {
self.fallback_strategy = Some(value);
self
}

/// Set `fallback_axis_side_direction` option.
pub fn fallback_axis_side_direction(mut self, value: Alignment) -> Self {
self.fallback_axis_side_direction = Some(value);
self
}

/// Set `flip_alignment` option.
pub fn flip_alignment(mut self, value: bool) -> Self {
self.flip_alignment = Some(value);
self
}
}

impl<Element: Clone> Default for FlipOptions<Element> {
fn default() -> Self {
Self {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/middleware/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ pub struct OffsetOptionsValues {
pub alignment_axis: Option<f64>,
}

impl OffsetOptionsValues {
/// Set `main_axis` option.
pub fn main_axis(mut self, value: f64) -> Self {
self.main_axis = Some(value);
self
}

/// Set `cross_axis` option.
pub fn cross_axis(mut self, value: f64) -> Self {
self.cross_axis = Some(value);
self
}

/// Set `alignment_axis` option.
pub fn alignment_axis(mut self, value: f64) -> Self {
self.alignment_axis = Some(value);
self
}
}

/// Options for [`Offset`] middleware.
///
/// A number (shorthand for [`main_axis`][`OffsetOptionsValues::main_axis`] or distance) or an axes configuration ([`OffsetOptionsValues`]).
Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/middleware/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ pub struct ShiftOptions<Element: Clone, Window: Clone> {
pub limiter: Option<Box<dyn Limiter<Element, Window>>>,
}

impl<Element: Clone, Window: Clone> ShiftOptions<Element, Window> {
/// Set `detect_overflow` option.
pub fn detect_overflow(mut self, value: DetectOverflowOptions<Element>) -> Self {
self.detect_overflow = Some(value);
self
}

/// Set `main_axis` option.
pub fn main_axis(mut self, value: bool) -> Self {
self.main_axis = Some(value);
self
}

/// Set `cross_axis` option.
pub fn cross_axis(mut self, value: bool) -> Self {
self.cross_axis = Some(value);
self
}

/// Set `limiter` option.
pub fn limiter(mut self, value: Box<dyn Limiter<Element, Window>>) -> Self {
self.limiter = Some(value);
self
}
}

impl<Element: Clone, Window: Clone> Default for ShiftOptions<Element, Window> {
fn default() -> Self {
Self {
Expand Down
35 changes: 35 additions & 0 deletions packages/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,41 @@ pub struct ComputePositionConfig<'a, Element, Window> {
pub middleware: Option<Vec<Box<dyn Middleware<Element, Window>>>>,
}

impl<'a, Element, Window> ComputePositionConfig<'a, Element, Window> {
pub fn new(platform: &'a dyn Platform<Element, Window>) -> Self {
ComputePositionConfig {
platform,
placement: None,
strategy: None,
middleware: None,
}
}

/// Set `platform` option.
pub fn platform(mut self, value: &'a dyn Platform<Element, Window>) -> Self {
self.platform = value;
self
}

/// Set `placement` option.
pub fn placement(mut self, value: Placement) -> Self {
self.placement = Some(value);
self
}

/// Set `strategy` option.
pub fn strategy(mut self, value: Strategy) -> Self {
self.strategy = Some(value);
self
}

/// Set `middleware` option.
pub fn middleware(mut self, value: Vec<Box<dyn Middleware<Element, Window>>>) -> Self {
self.middleware = Some(value);
self
}
}

/// Return of [`compute_position`][crate::compute_position::compute_position].
#[derive(Clone, Debug)]
pub struct ComputePositionReturn {
Expand Down
36 changes: 12 additions & 24 deletions packages/dom/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,18 @@ fn run() -> Result<(), JsValue> {
} = compute_position(
button,
tooltip,
Some(ComputePositionConfig {
placement: Some(Placement::Top),
strategy: None,
middleware: Some(vec![
Box::new(Offset::new(OffsetOptions::Value(6.0))),
Box::new(Flip::new(FlipOptions::default())),
Box::new(Shift::new(ShiftOptions {
detect_overflow: Some(DetectOverflowOptions {
boundary: None,
root_boundary: None,
element_context: None,
alt_boundary: None,
padding: Some(Padding::All(5.0)),
}),
main_axis: None,
cross_axis: None,
limiter: None,
})),
Box::new(Arrow::new(ArrowOptions {
element: arrow.clone().into(),
padding: None,
})),
]),
}),
Some(
ComputePositionConfig::default()
.placement(Placement::Top)
.middleware(vec![
Box::new(Offset::new(OffsetOptions::Value(6.0))),
Box::new(Flip::new(FlipOptions::default())),
Box::new(Shift::new(ShiftOptions::default().detect_overflow(
DetectOverflowOptions::default().padding(Padding::All(5.0)),
))),
Box::new(Arrow::new(ArrowOptions::new(arrow.clone().into()))),
]),
),
);

let arrow_data: Option<ArrowData> = middleware_data.get_as(ARROW_NAME);
Expand Down
12 changes: 6 additions & 6 deletions packages/dom/src/auto_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn observe_move(_element: &Element, _on_move: Rc<dyn Fn()>) -> Box<dyn Fn()> {
Box::new(|| {})
}

/// Options for [`auto_update``].
/// Options for [`auto_update`].
#[derive(Clone, Debug, Default)]
pub struct AutoUpdateOptions {
/// Whether to update the position when an overflow ancestor is scrolled.
Expand Down Expand Up @@ -62,31 +62,31 @@ pub struct AutoUpdateOptions {
}

impl AutoUpdateOptions {
/// Set [`Self::ancestor_scroll`] option.
/// Set `ancestor_scroll` option.
pub fn ancestor_scroll(mut self, value: bool) -> Self {
self.ancestor_scroll = Some(value);
self
}

/// Set [`Self::ancestor_resize`] option.
/// Set `ancestor_resize` option.
pub fn ancestor_resize(mut self, value: bool) -> Self {
self.ancestor_resize = Some(value);
self
}

/// Set [`Self::element_resize`] option.
/// Set `element_resize` option.
pub fn element_resize(mut self, value: bool) -> Self {
self.element_resize = Some(value);
self
}

/// Set [`Self::layout_shift`] option.
/// Set `layout_shift` option.
pub fn layout_shift(mut self, value: bool) -> Self {
self.layout_shift = Some(value);
self
}

/// Set [`Self::animation_frame`] option.
/// Set `animation_frame` option.
pub fn animation_frame(mut self, value: bool) -> Self {
self.animation_frame = Some(value);
self
Expand Down
20 changes: 20 additions & 0 deletions packages/dom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ pub struct ComputePositionConfig {
pub middleware: Option<MiddlewareVec>,
}

impl ComputePositionConfig {
/// Set `placement` option.
pub fn placement(mut self, value: Placement) -> Self {
self.placement = Some(value);
self
}

/// Set `strategy` option.
pub fn strategy(mut self, value: Strategy) -> Self {
self.strategy = Some(value);
self
}

/// Set `middleware` option.
pub fn middleware(mut self, value: MiddlewareVec) -> Self {
self.middleware = Some(value);
self
}
}

/// Computes the `x` and `y` coordinates that will place the floating element next to a given reference element.
pub fn compute_position(
reference: &Element,
Expand Down
5 changes: 1 addition & 4 deletions packages/leptos/example/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ pub fn App() -> impl IntoView {
let floating_arrow = create_node_ref::<Div>();

let middleware: MiddlewareVec = vec![
Box::new(Arrow::new(ArrowOptions {
element: floating_arrow,
padding: None,
})),
Box::new(Arrow::new(ArrowOptions::new(floating_arrow))),
Box::new(Offset::new(OffsetOptions::Value(4.0))),
];

Expand Down
Loading

0 comments on commit 492123a

Please sign in to comment.